-1

I have a sequence of lists such as :

>>> result


  [['Human_COII_1000-4566_hsp', 'Human_COII_500-789_hsp', 'Human_COII_100-300_hsp'], ['Human_COI_100-300_hsp', 'Human_COI_500-789_hsp', 'Human_COI_1000-4566_hsp']]

and I would like with each list to sort them by the number-number and get:

[['Human_COII_100-300_hsp', 'Human_COII_500-789_hsp', 'Human_COII_1000-4566_hsp'], ['Human_COI_100-300_hsp', 'Human_COI_500-789_hsp', 'Human_COI_1000-4566_hsp']]

I tried:

for i in result:
    sorted(i)

but the order is not the one I wanted.

David Jones
  • 4,766
  • 3
  • 32
  • 45
chippycentra
  • 879
  • 1
  • 6
  • 15

1 Answers1

1

You could make a new sorted list using comprehension,

>>> import re
>>> x
[set(['Human_COII_1000-4566_hsp', 'Human_COII_100-300_hsp', 'Human_COII_500-789_hsp']), set(['Human_COI_100-300_hsp', 'Human_COI_500-789_hsp', 'Human_COI_1000-4566_hsp'])]
>>>
>>> # for python2
>>> [sorted(y, key=lambda item: map(int, re.findall(r'\d+', item))) for y in x]
[['Human_COII_100-300_hsp', 'Human_COII_500-789_hsp', 'Human_COII_1000-4566_hsp'], ['Human_COI_100-300_hsp', 'Human_COI_500-789_hsp', 'Human_COI_1000-4566_hsp']]
>>>
>>> # python3
>>> [sorted(y, key=lambda item: tuple(map(int, re.findall(r'\d+', item)))) for y in x]
han solo
  • 6,390
  • 1
  • 15
  • 19