简体   繁体   中英

Find unique elements in tuples in a python list

Is there a better way to do this in python, or rather: Is this a good way to do it?

x = ('a', 'b', 'c')
y = ('d', 'e', 'f')
z = ('g', 'e', 'i')

l = [x, y, z]

s = set([e for (_, e, _) in l])

I looks somewhat ugly but does what i need without writing a complex "get_unique_elements_from_tuple_list" function... ;)

edit: expected value of s is set(['b','e'])

That's fine, that's what sets are for. One thing I would change is this:

s = set(e[1] for e in l)

as it enhances readability. Note that I also turned the list comprehension into a generator expression; no need to create a temporary list.

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