How do I sort these?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Oct 30 03:30:22 EST 2005


# This can be a solution:

from operator import itemgetter
seq1a = ([2] * 4) + ([1] * 4)
seq2a = [complex(3-el,7-el) for el in range(1, 9)]
assert len(seq1a) == len(seq2a)
print seq1a, seq2a, "\n"

mix = zip(seq1a, seq2a)
# mix.sort() # Not possible
mix.sort(key=itemgetter(0))

# If you need tuples output:
seq1b, seq2b = zip(*mix)
print seq1b, seq2b, "\n"

# Alternative, lists output:
seq1b = [el[0] for el in mix]
seq2b = [el[1] for el in mix]
print seq1b, seq2b, "\n"

Bye,
bearophile




More information about the Python-list mailing list