0

I have a list in this format:

exon_start exon_finish gene_name (repeated hundreds of times)

I want to sort by exon_start

Example list:

 ['8342758', '8344137', 'NM_001042682']
 ['85420368', '85421471', 'NM_032184']
 ['86363115', '86364485', 'NM_152890']
 ['89820771', '89822936', 'NM_015350']
 ['904123', '905900', 'NR_027693']
 ['91176416', '91179454', 'NM_201269']
 ['92418409', '92420740', 'NM_015237']
 ['93575521', '93577419', 'NR_034089']
 ['94114411', '94116006', 'NM_014597']
 ['99926918', '99928016', 'NM_017734']

This list of lists (printed above) has already been sorted with the following code:

sorted_triplets = sorted(triplets, key=lambda x: x[0])
for i in sorted_triplets:
    print i

However, "sorted" isn't working like I expect. As you can see from the list, 904123 is less than 89820771. So it appears that "sorted" isn't comparing the numbers as a whole, rather as individual digits.

How do I fix this?

6 Answers 6

4

It's sorting them as strings, so the order is 'alphabetically'. Which is to say, it's going character by character and comparing, instead of comparing them as scalar values.

So do:

sorted_triplets = sorted(triplets, key=lambda x: int(x[0]))

And it should work.

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

3 Comments

I've partially taught myself perl, then thought, "why not try python". I'm still getting used to having to explicitly tell python what is an integer.
I don't know a lot of perl, but I always hated the 'automatic conversion' stuff in PHP. I like a language that forces me to be explicit.
Sure, but in that case why don't you have to explicitly define whether a data type is a tuple, list, or dictionary? (This is a rhetorical question - I'm not looking for an answer)
1

convert strings to numbers

sorted(triplets, key=lambda x: int(x[0]))

Comments

0

Convert exon_start to an integer, strings are sorted lexicographically.

Comments

0

Right, because what you have are strings, not numbers. It will be sorted lexicographically. You may want to convert them to numbers (integers) first.

Comments

0

It seems like your "number" is actually a string. Convert this string to an integer ( int(string) ), then the sorting should work

Comments

0

Add int() call.

sorted_triplets = sorted(triplets, key=lambda x: int(x[0]))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.