Note that there are some explanatory texts on larger screens.

plurals
  1. POpython sort list of lists with casting
    primarykey
    data
    text
    <p>I know tat similar questions has been asked already several times. And i do now how to use the search function, but it still does not work. </p> <p>So here is the problem setup. I have a list of lists containing strings. One column contains strings which actually represent float values. And it is also the column i want to sort by. The problem is, that python seems to ignore the - (minus) sign on entries. So an example list of: </p> <pre><code>[[blaa, '0.3', bli], [bla, '0.1', blub], [bla, '-0.2', blub]] </code></pre> <p>gets sorted like this:</p> <pre><code>[[bla, '0.1', blub], [bla, '-0.2', blub], [blaa, '0.3', bli]] </code></pre> <p>and not how it should be:</p> <pre><code>[[bla, '-0.2', blub],[bla, '0.1', blub], [blaa, '0.3', bli]] </code></pre> <p>So far i have tried:</p> <ul> <li>casting the second column to float and sorting by that column</li> </ul> <p>like:</p> <pre><code>for i in mylist: i[1] = float(i[1]) mylist.sort(key=lambda x: x[1]) </code></pre> <p>or with</p> <pre><code>for i in mylist: i[1] = float(i[1]) mylist.sort(key=operator.itemgetter(1)) </code></pre> <ul> <li>I also tried to define my own compare function:</li> </ul> <p>like:</p> <pre><code>mylist.sort(cmp=lambda x,y: cmp(float(x), float(y)), key=operator.itemgetter(1)) </code></pre> <p>And any other combination of the above methods, also the same with <code>sorted</code>. So far with no success, the minus sign gets ignored every time. How do is solve this? </p> <p><strong>[edit]</strong> Also already tried the Ignacio suggestion. I should mention i HAVE to use python 2.5 .</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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