我有这样的元组列表:
lst = [(10, u'1.15', u'1.15'), (5, 0, u'1.875'), (3, u'2.28', u'2.28')]
我希望得到新元素,只有每个元组的第二个和第三个元素,不等于0,换句话说,我需要类似的东西:
new_lst = [u'1.15', u'1.15',u'1.875', u'2.28', u'2.28']
感谢您的回答。
答案 0 :(得分:4)
new_lst = [x for t in lst for x in t[1:] if x != 0]
答案 1 :(得分:2)
>>> [x for y in lst for x in y[1:3] if x]
[u'1.15', u'1.15', u'1.875', u'2.28', u'2.28']
答案 2 :(得分:0)
>>> L = [(10, u'1.15', u'1.15'), (5, 0, u'1.875'), (3, u'2.28', u'2.28')]
>>> answer = []
>>> for tup in L:
... answer.extend([i for i in tup[1:] if i])
...
>>> answer
[u'1.15', u'1.15', u'1.875', u'2.28', u'2.28']
希望这有帮助