Sorting Driving Crazy: URGENT: PLEASE HELP

dont bother dontbotherworld at yahoo.com
Sat Mar 13 01:38:28 EST 2004


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_element={}
new_feature_vector={}

for x in sorted_feature_vector:
    print "SORTING\n"
    print x, '\t',feature_vectors[x]
    count=0
    if(count==0):
        new_feature_element=str(1)+"
"+str(x)+":"+str(feature_vectors[x])+" "
    else:
       
new_feature_element=str(x)+":"+str(feature_vectors[x])+"
"
        count=count+1
       
new_feature_vector=str(new_feature_vector)+str(new_feature_element)
        new_feature_vector=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.00224466891134
SORTING

3105 	0.00224466891134
SORTING

3117 	0.00224466891134
SORTING

3675 	0.003367003367
SORTING

4280 	0.00224466891134
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 at midtoad.homelinux.org> wrote:
> dont bother wrote:
> 
> > 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
> that I
> > can apply .keys() method
> 
> 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(item,":")
>         mydict[a] = b
>         print mydict[a]
> print "Dictionary is: ", mydict
> ---
> 
> cheers
> Stewart
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com




More information about the Python-list mailing list