如何压缩此列表:
irr_list = [[[u'Animal Collective'], u'Lion in a Coma'], [[u'Animal Collective'], u'Hocus Pocus'], [[u'Animal Collective'], u'Vertical'], [[u'Animal Collective'], u'Natural Selection'], [[u'Animal Collective'], u'Spilling Guts'], [[u'Animal Collective'], u'Recycling']]
这个想要的输出?
output = [[u'Animal Collective', u'Lion in a Coma'], [u'Animal Collective', u'Hocus Pocus'], [u'Animal Collective', u'Vertical'], [u'Animal Collective', u'Natural Selection'], [u'Animal Collective', u'Spilling Guts'], [u'Animal Collective', u'Recycling']]
我试过了:
flattened = [val for sublist in irr_list for val in sublist]
无济于事
答案 0 :(得分:0)
你可以试试这个:
import itertools
new_data = [list(itertools.chain.from_iterable([[b] if not isinstance(b, list) else b for b in i])) for i in irr_list]
输出:
[[u'Animal Collective', u'Lion in a Coma'], [u'Animal Collective', u'Hocus Pocus'], [u'Animal Collective', u'Vertical'], [u'Animal Collective', u'Natural Selection'], [u'Animal Collective', u'Spilling Guts'], [u'Animal Collective', u'Recycling']]
答案 1 :(得分:0)
如果您的列表始终采用[[list, element], [list, element], ...]
格式,那么您可以使用列表推导
output = [[l[0], e] for l, e in irr_list]