I have some code that I find clumsy.
Given:
sample_lists = [(u'1', u'penguin'), (u'2', u'kelp'), (u'5', u'egg')],
[(u'3', u'otter'), (u'4', u'clam')]
I want the result: ['penguin', 'kelp', 'otter', 'clam', 'egg']
(ordered by number). You can assume that lists always contains '1'
, and each sub-list is in ascending order, and that all the numbers in sample_lists
are consecutive integers, if that helps.
Currently, the most pythonic/concise way I can think of doing this is:
sample_list = sample_lists[0]+sample_lists[1]
for i in xrange(len(sample_list)):
sample_list[0] = i+1
return zip(*sorted(sample_list, key=operator.itemgetter(0)))[1]
Is there a better way? I feel like this is quite clumsy. The problem here is sorting by the strings, which I need to convert over to ints one-by-one. A hunch would be some lambda function, but I'm not terribly well versed in that syntax. If this is the best way, I'm still curious as to how to do this using lambda functions, if possible.
Note that the direct string comparison one-liner:
print zip(*sorted(sample_lists[0]+sample_lists[1], key=operator.itemgetter(0)))[1]
Does not work, as it fails if sample_lists contains '11' or '20'.
A python 3 answer is okay, but python 2.7 is preferred.