0

I have a python list of lists like this

[['CCND1', '67', 'FAS', '99', 'IRAK3', '92', 'ALG14', '86', 'ADRBK1', '10'], ['PTRX', '95', 'CCNA', '33']]

Each alphabetical value is associated with the numeric value , i.e IRAK3 and 92 are associated (92, should appear after IRAK3) and PTRX and 95 are associated (95 should appear after PTRX ). Now , I want to alphabetically sort this list of lists so that the sorted list looks like this:

[['ADRBK1', '10', 'ALG14', '86', 'CCND1', '67', 'FAS', '99', 'IRAK3', '92' ], ['CCNA', '33', 'PTRX', '95' ]]

Note that in the sorted list, the alphabetical values are sorted but again, note that 92, appear after IRAK3 AND 95 appear after PTRX i.e the association is maintained.

How could I do that ?

lakhujanivijay
  • 107
  • 2
  • 11
  • 2
    Why did you put associated values in separate list elements, instead of combining them into a tuple or dict? – Barmar Jan 07 '20 at 07:40
  • 2
    Which specific part of that are you having trouble with? I'd suggest splitting into pairs `("ADRBK1", "10")` and sorting those, then flattening it again. – jonrsharpe Jan 07 '20 at 07:40
  • Lists should generally be homogeneous. Use tuples and dictionaries for heterogeneous collections of related data. – Barmar Jan 07 '20 at 07:41
  • 2
    What have you tried so far? SO isn't a code-writing service – Pynchia Jan 07 '20 at 08:14
  • This question is clear. Don't close it as unclear. (Witness the three to-the-point answers.) If you don't like the zero effort, then downvote. – Jean-François Corbett Jan 07 '20 at 14:45

3 Answers3

1

This is one approach.

Ex:

from itertools import chain

data = [['CCND1', '67', 'FAS', '99', 'IRAK3', '92', 'ALG14', '86', 'ADRBK1', '10'], ['PTRX', '95', 'CCNA', '33']]
#pair elements --> [('CCND1', '67'), ('FAS', '99')....
data = [zip(i[::2], i[1::2]) for i in data]
#sort and flatten 
data = [list(chain.from_iterable(sorted(i, key=lambda x: x[0]))) for i in data]
print(data)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

"Lists should generally be homogeneous. Use tuples and dictionaries for heterogeneous collections of related data." [1]

One approach would be you arrange them as tuples and then sorting them would also be easier.

mylist = [['CCND1', '67', 'FAS', '99', 'IRAK3', '92', 'ALG14', '86', 'ADRBK1', '10'], ['PTRX', '95', 'CCNA', '33']]

list_of_tuples = []
for l in mylist:
    if len(l) % 2 is not 0:
        raise ValueError()
    list_of_tuples.append([(l[i], l[i+1]) for i in range(0, len(l), 2)])

for l in list_of_tuples:
    l.sort(key=lambda tup: tup[0])

print(list_of_tuples)

# [[('ADRBK1', '10'), ('ALG14', '86'), ('CCND1', '67'), ('FAS', '99'), ('IRAK3', '92')], [('CCNA', '33'), ('PTRX', '95')]]
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

You can try this:

>>> list_ = [['CCND1', '67', 'FAS', '99', 'IRAK3', '92', 'ALG14', '86', 'ADRBK1', '10'], ['PTRX', '95', 'CCNA', '33']]

>>> paired_list = (zip(*[iter(l)]*2) for l in list_)

>>> sorted_list = [list(sum(sorted(l, key=lambda x: x[0]),())) for l in paired_list]

>>> sorted_list

[['ADRBK1', '10', 'ALG14', '86', 'CCND1', '67', 'FAS', '99', 'IRAK3', '92'],
 ['CCNA', '33', 'PTRX', '95']]

References:

  1. zip(*[iter(iterable)]*n)

  2. sorted

  3. I have used sum to concatenate the list of tuples. However, if you are prepared to use other modules, it is always better to use itertools.chain.from_iterables.

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52