python tuple list sorting

Question!

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



Answers

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.


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)
By : Dave Webb


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 :-)

By : Gregory


This video can help you solving your question :)
By: admin