473,883 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 <st*****@midtoa d.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(it em,":")
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 2536
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******* *************** *************** *@python.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_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


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
Jul 18 '05 #3
On 13 Mar 2004 03:26:00 -0800, xt***@toysinaba g.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******* *************** *************** *@python.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.0022446689113 4
SORTING

3105 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

....

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
1614
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 right. The text makes the other fields move when it is shown - even though the width of the text is less than the width of the field. WHY? This is driving me nuts - please help! TIA,
8
5282
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 bit of background: The company I work for is developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
2
368
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. It is easy to reproduce the problem I have. If you create a new Windows Application Put a DataGrid control on it. Put the following in the onload event of the form:
0
1313
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 = Request.Cookies("MyCookie") If Not cookie Is Nothing Then Response.Write(" * Cookie Exists * ") If Not cookie.Values("culture") Is Nothing
14
1777
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: '.$_SESSION.'<br />'; print '$_SESSION: '.$_SESSION.'<br />'; print $_SESSION." - ".$_SESSION.' = '.($_SESSION-$_SESSION);
3
2287
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. I'm trying to create a runner for informix which includes the function in order to allow me to call it from within a 4gl program. When i do this i get differences in the key that is produced????? This only happens when metahpone translates an x in a name. The stand
7
2540
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 to um, sort an array. I have run into some trouble though. On lines 54-59, I previously had them placed from line 46 on. This worked out great for printing just the bubble sort, but I have to get the other four sorting algorithms in here and I was hoping to print the contents out in a for loop, as...
4
1863
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 resolution failed because no accessible 'New' accepts this number of arguments. " and my code is Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick If e.Column <> sortColumn Then sortColumn = e.Column...
2
3636
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 the page theme. Long story short, my "Text Posts" are supposed to be in the font: Georgia, but they are showing up in "Times New Roman"...blah! I can't find anything wrong in the code, but who am I trying to fool? I know nothing about this stuff. The code is below. The parts that I *think*...
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5784
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3228
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.