By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

Sorting Driving Crazy: URGENT: PLEASE HELP

P: n/a
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 <st*****@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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
dont bother <do*************@yahoo.com> writes:
When I print x and feature_vectors[x] I get some of
the entries which are not in the increasing order.


(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'.
Jul 18 '05 #2

P: n/a
dont bother <do*************@yahoo.com> wrote in message news:<ma**************************************@pyt hon.org>...
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


The keys of your feature_vectors dictionary are strings - they have
been sorted lexicographically. 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_vectors = {}
for key, value in feature_vectors:
new_feature_vectors[int(key)] = value

Then new_feature_vectors.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
Jul 18 '05 #3

P: n/a
On 13 Mar 2004 03:26:00 -0800, xt***@toysinabag.com (xtian) wrote:
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.


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
Jul 18 '05 #4

P: n/a
dont bother <do*************@yahoo.com> wrote in message news:<ma**************************************@pyt hon.org>...
This is really driving me crazy. ....

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

22846 93.1537598204
SORTING

2357 0.00224466891134
SORTING

3105 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

....

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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.