有:
my_list = [([9, 1, 4], ['b', 'j', 'k']), ([8, 5, 9], ['m', 't', 'y']), ([6, 2, 0], ['p', 'q', 'r'])]
想要(按顺序):
number_list = [9, 1, 4, 8, 5, 9, 6, 2, 0]
letter_list = ['b', 'j', 'k', 'm', 't', 'y', 'p', 'q', 'r']
明显的解决方案是:
for data in my_list:
try:
number_list += data[0]
letter_list += data[1]
except:
number_list = data[0]
letter_list = data[1]
print number_list
print letter_list
> [9, 1, 4, 8, 5, 9, 6, 2, 0]
> ['b', 'j', 'k', 'm', 't', 'y', 'p', 'q', 'r']
是否有更好和/或更快的方法来实现这一目标 - 可能是列表理解?
答案 0 :(得分:6)
>>> from itertools import chain
>>> a, b = [list(chain.from_iterable(x)) for x in zip(*my_list)]
>>> a
[9, 1, 4, 8, 5, 9, 6, 2, 0]
>>> b
['b', 'j', 'k', 'm', 't', 'y', 'p', 'q', 'r']