简体   繁体   中英

how to append multiple elements to a new list by filter an old one

I have a list like [ ((xs,xe),idx),...]

I want to collect xs and xe into a set,like this way

s={ xs,xe for ((xs,xe),idx)

I know I can use it two times and add xs,xe respectively.

Is there a better way to do it?

You can use itertools.chain.from_iterable

>>> import itertools
>>> lst = [((1,2), 'a'), ((3,4), 'b')]
>>> s = set(itertools.chain.from_iterable(pair for pair, _ in lst))
>>> s
set([1, 2, 3, 4])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM