1

so I have a list with a whole bunch of tuples

j = 

[('jHKT', 'Dlwp Dfbd Gwlgfwqs (1kkk)', 53.0),
('jHKT', 'jbdbjf Bwvbly (1kk1)', 35.0),
('jHKT', 'Tfstzfy (2006)', 9.0),
('jHKT', 'fjznfnt Dwjbzn (1kk1)', 25.0),
('jHKT', 'Vznbsq sfnkz (1k8k)', 4.0),
('jHKT', 'fxzt, Clwwny! (2005)', 8.0),
('jHKT', "Dwfs Thzs jfbn Wf'lf jbllzfd? (1kk1)", 12.0),
('jHKT', 'Chbzljbn wf thf Bwbld (1kk8)', 30.0),
('jHKT', 'Vblfdzctzwn (2006)', 8.0),
('jHKT', 'jwltbl Kwjbbt (1kk5)', 13.0)]

and I tried to sort it using the third element of the tuple as the index:

note that the list above is just a partial list...the actual list contains thousands of elements

anyways so I did:

j = sorted(j, key=lambda e : e[2])

but then when I do that, it ends up messing up the third element of the tuple and I highly doubt that it actually sorted...here's another partial list of the output

('jHKT', 'Frz yzng (2004)', 0.0)
('jHKT', 'kff thr Mvp (2003)', 0.0)
('jHKT', 'HzpHkpBvttlr.ckm: Hzp Hkp 4 Lzfr (2001)', 0.0)
('jHKT', 'z Wvlk thr Lznr (1970)', 0.0)
('jHKT', '1971: erzsknrrs kf svr (2007)', 0.0)
('jHKT', 'Wzld Rzdr, Thr (1960)', 0.0)
('jHKT', 'Dzshdkgz (2005)', 0.0)
('jHKT', 'Lzttlr Thzngs, Thr (2006)', 0.0)
('jHKT', 'Trrmznvl rrrkr (2002)', 0.0)
('jHKT', 'Hqngry Bvchrlkrs Clqb, Thr (1999)', 0.0)
('jHKT', 'Swrrt Lkvr, Bzttrr (1967)', 0.0)
('jHKT', 'Trn tk Chz tk (1990)', 0.0)
('jHKT', 'Bvr-Crl-knv (1987)', 0.0)
('jHKT', 'Rknny & Czndy zn vll kf qs (2006)', 0.0)

in this case, it ended up resetting all of the third element of the tuples into 0...

what did I do wrong??

I'm using python 3

##############################EDIT####################################

also, when I tried to print the list of tuples, it would return this error:

  print(j)
IOError: [Errno 22] Invalid argument

and the printing would abruptly stop...:

 ('sadfasdf (1991)', 'xcvwert (1985)', 0.0), ('r3sdaf (1991)', 'jkzxkk (1993)', 0.0), ('werwww (1991)', 'Third WhTraceback (most recent call last):

and then the error appears

################EDIT###################

On the other hand, printing the list by iterating works just fine

so

for i in j:
    print(i)

works fine whereas just print(j) would return that error

10
  • Your code works fine for me in Python 3.1.2. Commented Mar 30, 2011 at 6:16
  • Same here, "works for me." Are you sure you the list you are sorting actually has those values? Perhaps you have many items (in that 1000+ list that have zeros). The errors you have seem to be a separate issue which could be a possible cause here. Is the sorting the problem you want to take care of or that error? Commented Mar 30, 2011 at 6:43
  • code works fine in Python 2.6 Commented Mar 30, 2011 at 6:43
  • yeah the code I wrote above works for me too, the problem is probably with the list but I can't post the list here since it's like thousands of lines long...If I use the list above it works, but if I use the actual list, it doesn't work Commented Mar 30, 2011 at 6:44
  • I did print before the sort and another print right after the sort so no other code could have interfered Commented Mar 30, 2011 at 6:45

4 Answers 4

1

I think your code works correctly and you see the first part of the list, where key is realy 0.0. You just sort the list in ascending order :-)

Sign up to request clarification or add additional context in comments.

Comments

1

i too think its ok[in quick glance] .. check this link .. it's about various sorting techniques in python

http://wiki.python.org/moin/HowTo/Sorting/

Comments

0

As others said, the code is perfectly fine. You should try to isolate the situation and try to find out where exactly the issue happened.

  • Does it happen in a simple script which only contains the list assignment and the sort operation?
  • Do other list operations work? Try slicing, iterating over it, or sorting without a custom function.
  • Does it happen in a slice of the current list? Bisection method is your friend here.

1 Comment

please add a link to that Bisection method
0

It's probably worth comparing the sorted and unsorted lists to see if the sort is actually changing data. You could try something as simple as:

print sum(e[2] for e in j)
j = sorted(j, key=lambda e : e[2])
print sum(e[2] for e in j)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.