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
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
Comment