Skip to content Skip to sidebar Skip to footer

How Do I Sort By The First Element Of Each Line?

I am attempting to sort the first element of each line alphabetically but struggling to get this to work. [['^', 'G', 'A', 'T', 'T', 'A', 'C', 'A', '!']] [['G', 'A', 'T', 'T', 'A',

Solution 1:

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

Or...

from operatorimport itemgetter
sort = sorted(data, key=itemgetter(0))

Change the loop to this:

rotations = []
for seq1 in range(len(list1)):
    table1 = list1[seq1:] + list1[:seq1]
    rotations.append(table1)
rotations = sorted(rotations, key=lambda x: (x[0] not in string.ascii_letters, x[0]))
print([x[-1] for x in rotations])

Post a Comment for "How Do I Sort By The First Element Of Each Line?"