472,328 Members | 1,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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_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
4 2338
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Keiron Waites | last post by:
Please see the problem in action here: http://www.leadbullet.biz/contact.php If you mouse over the fields, you will see that text is shown on the...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a...
2
by: ChrisM | last post by:
Can anyone please tell me what I'm doing wrong here. I have a Windows Form with a DataGrid on it, and I'm having real problems with the Sorting. ...
0
by: Shapper | last post by:
Hello, I have this code in Global.asax: Sub Session_Start(Sender As Object, E As EventArgs) Dim cookie As HttpCookie =...
14
by: mat | last post by:
Hi all, This is weird and it is driving me nuts, please can someone explain how I can get it to return the correct answer: print '$_SESSION:...
3
by: rashpal.sidhu | last post by:
Please help, this problem is driving me crazy !! I am using metaphone to create phonetic keys. When i run the module stand-a-lone it works fine....
7
beacon
by: beacon | last post by:
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes...
4
by: rajtalent | last post by:
hi all, I want to sort the colum when clicks the columnheader using vb.net 2005 .But i receive the following error "Error 1 Overload...
2
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.