Note that there are some explanatory texts on larger screens.

plurals
  1. POpython tuple list sorting
    text
    copied!<p>so I have a list with a whole bunch of tuples</p> <pre><code>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)] </code></pre> <p>and I tried to sort it using the third element of the tuple as the index:</p> <p>note that the list above is just a partial list...the actual list contains thousands of elements</p> <p>anyways so I did: </p> <pre><code>j = sorted(j, key=lambda e : e[2]) </code></pre> <p>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</p> <pre><code>('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 &amp; Czndy zn vll kf qs (2006)', 0.0) </code></pre> <p>in this case, it ended up resetting all of the third element of the tuples into 0...</p> <p>what did I do wrong??</p> <p>I'm using python 3</p> <p><strong>##############################EDIT####################################</strong></p> <p>also, when I tried to print the list of tuples, it would return this error:</p> <pre><code> print(j) IOError: [Errno 22] Invalid argument </code></pre> <p>and the printing would abruptly stop...:</p> <pre><code> ('sadfasdf (1991)', 'xcvwert (1985)', 0.0), ('r3sdaf (1991)', 'jkzxkk (1993)', 0.0), ('werwww (1991)', 'Third WhTraceback (most recent call last): </code></pre> <p>and then the error appears</p> <p><strong>################EDIT###################</strong></p> <p>On the other hand, printing the list by iterating works just fine</p> <p>so </p> <pre><code>for i in j: print(i) </code></pre> <p>works fine whereas just print(j) would return that error</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload