Sorting Driving Crazy: URGENT: PLEASE HELP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dont bother

    Sorting Driving Crazy: URGENT: PLEASE HELP

    This is really driving me crazy.

    I have a dictionary feature_vectors {}. I try to sort
    its keys using

    #apply sorting on feature_vectors
    sorted_feature_ vector=feature_ vectors.keys()
    sorted_feature_ vector.sort()

    #feature_vector .keys()=sorted_ feature_vector

    new_feature_ele ment={}
    new_feature_vec tor={}

    for x in sorted_feature_ vector:
    print "SORTING\n"
    print x, '\t',feature_ve ctors[x]
    count=0
    if(count==0):
    new_feature_ele ment=str(1)+"
    "+str(x)+":"+st r(feature_vecto rs[x])+" "
    else:

    new_feature_ele ment=str(x)+":" +str(feature_ve ctors[x])+"
    "
    count=count+1

    new_feature_vec tor=str(new_fea ture_vector)+st r(new_feature_e lement)
    new_feature_vec tor=new_feature _vector+"\n"

    print "New Dictionary"


    When I print x and feature_vectors[x] I get some of
    the entries which are not in the increasing order.

    22836 7.46464646465
    SORTING

    22840 2.19977553311
    SORTING

    22841 2.69809203143
    SORTING

    22842 0.617283950617
    SORTING

    22844 4.61279461279
    SORTING

    22846 93.1537598204
    SORTING

    2357 0.0022446689113 4
    SORTING

    3105 0.0022446689113 4
    SORTING

    3117 0.0022446689113 4
    SORTING

    3675 0.003367003367
    SORTING

    4280 0.0022446689113 4
    SORTING






    This should not happen but why is this happening and
    how to fix this.

    My problem is given a dictionary, sort the keys and
    have a new dictionary with the key, values where the
    keys are in increasing order.

    I would be really grateful if anyone can help me out

    Thanks
    Dont



    --- midtoad <stewart@midtoa d.homelinux.org > wrote:[color=blue]
    > dont bother wrote:
    >[color=green]
    > > I have a string:
    > >
    > > feature_vector. It is of the form
    > > <index: value, index: value, index: value>
    > >
    > > I want to make this string into a dictionary so[/color]
    > that I[color=green]
    > > can apply .keys() method[/color]
    >
    > okay, here's a solution, assuming that your < and >
    > are part of the string.
    > If not, remove the line where you take a slice.
    > I'm sure that there are
    > more Pythonic solutions, but this works...
    >
    > ---
    > import string
    >
    > # define a test string
    > s1 = "<name1: value1, name2: value2, name3: value3>"
    > # get rid of the < and > by taking a slice
    > s1 = s1[1:-1]
    > # split string at the commas
    > s2 = string.split(s1 ,',')
    > mydict = {}
    > for item in s2:
    > a,b = string.split(it em,":")
    > mydict[a] = b
    > print mydict[a]
    > print "Dictionary is: ", mydict
    > ---
    >
    > cheers
    > Stewart
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list[/color]


    _______________ _______________ ____
    Do you Yahoo!?
    Yahoo! Mail - More reliable, more storage, less spam
    It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got your back with other convenient features like one-tap unsubscribe, free trial expiration alerts and package tracking


  • Paul Rubin

    #2
    Re: Sorting Driving Crazy: URGENT: PLEASE HELP

    dont bother <dontbotherworl d@yahoo.com> writes:[color=blue]
    > When I print x and feature_vectors[x] I get some of
    > the entries which are not in the increasing order.[/color]

    (keys shown are: 22836 22840 22841 22842 22844 22846 2357 3105 3117
    3675 4280)

    Those keys are sorted correctly. You have entered them in the
    dictionary as strings, not integers. '22836' comes before '4280' just
    like 'apple' comes before 'pear'.

    Comment

    • xtian

      #3
      Re: Sorting Driving Crazy: URGENT: PLEASE HELP

      dont bother <dontbotherworl d@yahoo.com> wrote in message news:<mailman.3 46.1079159911.1 9534.python-list@python.org >...[color=blue]
      > This is really driving me crazy.
      >
      > I have a dictionary feature_vectors {}. I try to sort
      > its keys using
      >
      > #apply sorting on feature_vectors
      > sorted_feature_ vector=feature_ vectors.keys()
      > sorted_feature_ vector.sort()
      >
      > #feature_vector .keys()=sorted_ feature_vector
      >
      > new_feature_ele ment={}
      > new_feature_vec tor={}
      >
      > for x in sorted_feature_ vector:
      > print "SORTING\n"
      > print x, '\t',feature_ve ctors[x]
      > count=0
      > if(count==0):
      > new_feature_ele ment=str(1)+"
      > "+str(x)+":"+st r(feature_vecto rs[x])+" "
      > else:
      >
      > new_feature_ele ment=str(x)+":" +str(feature_ve ctors[x])+"
      > "
      > count=count+1
      >
      > new_feature_vec tor=str(new_fea ture_vector)+st r(new_feature_e lement)
      > new_feature_vec tor=new_feature _vector+"\n"
      >
      > print "New Dictionary"
      >
      >
      > When I print x and feature_vectors[x] I get some of
      > the entries which are not in the increasing order.
      >
      > 22836 7.46464646465
      > SORTING
      >
      > 22840 2.19977553311
      > SORTING
      >
      > 22841 2.69809203143
      > SORTING
      >
      > 22842 0.617283950617
      > SORTING
      >
      > 22844 4.61279461279
      > SORTING
      >
      > 22846 93.1537598204
      > SORTING
      >
      > 2357 0.0022446689113 4
      > SORTING
      >
      > 3105 0.0022446689113 4
      > SORTING
      >
      > 3117 0.0022446689113 4
      > SORTING
      >
      > 3675 0.003367003367
      > SORTING
      >
      > 4280 0.0022446689113 4
      > SORTING
      >[/color]

      The keys of your feature_vectors dictionary are strings - they have
      been sorted lexicographical ly. The string "22846" is less than the
      string "2357".

      To check this, you can put:

      print "Type of x is", type(x)

      into your loop. It will be <type 'str'>, while you want <type 'int'>.

      To convert feature_vectors into a dictionary with integer keys, use
      this:

      new_feature_vec tors = {}
      for key, value in feature_vectors :
      new_feature_vec tors[int(key)] = value

      Then new_feature_vec tors.keys() should be able to be sorted to have
      the order you want.

      On a side note, you seem to be having a lot of difficulty getting a
      program to do what you want - have you worked through the tutorial
      <http://www.python.org/doc/current/tut/tut.html>? The interactive
      prompt can be very useful to check that snippets of code do what you
      think they do.

      Cheers,
      xtian

      Comment

      • Dang Griffith

        #4
        Re: Sorting Driving Crazy: URGENT: PLEASE HELP

        On 13 Mar 2004 03:26:00 -0800, xtian@toysinaba g.com (xtian) wrote:
        [color=blue]
        >On a side note, you seem to be having a lot of difficulty getting a
        >program to do what you want - have you worked through the tutorial
        ><http://www.python.org/doc/current/tut/tut.html>? The interactive
        >prompt can be very useful to check that snippets of code do what you
        >think they do.[/color]

        dontbother, I think this is a good question. You've been posting a
        lot of questions that are covered in the tutorial. I'm willing to
        help kids with their homework, but not until they've not read the
        assignment.

        If you went through the tutorial in the standard distribution, you
        might want to look at http://www.awaretek.com/tutorials.html for more
        tutorials. There are currently 77 tutorials listed on that site.

        --dang

        Comment

        • TomH

          #5
          Re: Sorting Driving Crazy: URGENT: PLEASE HELP

          dont bother <dontbotherworl d@yahoo.com> wrote in message news:<mailman.3 46.1079159911.1 9534.python-list@python.org >...[color=blue]
          > This is really driving me crazy.[/color]
          ....[color=blue]
          >
          >
          > When I print x and feature_vectors[x] I get some of
          > the entries which are not in the increasing order.[/color]
          ....[color=blue]
          >
          > 22844 4.61279461279
          > SORTING
          >
          > 22846 93.1537598204
          > SORTING
          >
          > 2357 0.0022446689113 4
          > SORTING
          >
          > 3105 0.0022446689113 4
          > SORTING[/color]
          ....[color=blue]
          >
          > This should not happen but why is this happening and
          > how to fix this.
          >
          > My problem is given a dictionary, sort the keys and
          > have a new dictionary with the key, values where the
          > keys are in increasing order.
          >
          > I would be really grateful if anyone can help me out
          >
          > Thanks
          > Dont
          >[/color]
          ....

          Your keys are strings, so they're being sorted in alphabetical order
          e.g. 'a', 'aa', 'b'. It doesn't matter if all of the characters in the
          strings happen to be digits, e.g. '1', '11', '2', strings are strings.
          Cast the keys to integers before the sort.

          -Tom

          Comment

          Working...