开发者

Python, working with lists

I have such list of tuples:

lst = [(10, u'1.15', u'1.15'), (5, 0, u'1.875'), (3, u'2.28', u'2.28')]

and I want to get the new one with just second and开发者_运维知识库 third element of each tuple, which not equal to the 0, in other words, I need something like:

new_lst = [u'1.15', u'1.15',u'1.875', u'2.28', u'2.28']

Thanks for your answers.


new_lst = [x for t in lst for x in t[1:] if x != 0]


>>> [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']


>>> 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']

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜