简体   繁体   中英

What is the most Pythonic way to test for match with first item of tuple in sequence of 2-tuples?

Suppose you have a sequence of 2-tuples:

seq_of_tups = (('a', 1), ('b', 2), ('c', 3))

and you want to test if 'a' is the first item of any tuple in the sequence.

What is the most Pythonic way?

Convert to a dictionary and test for keys, which seems easy enough to understand? ie

'a' in dict(seq_of_tups)

Use a cute zip trick which is is not particularly clear unless you know the trick? ie

'a' in zip(*seq_of_tups)[0]

Or be really explicit with map? ie

'a' in map(lambda tup: tup[0], seq_of_tups)

Or is there a better way than any of these choices?

>>> seq_of_tups = (('a', 1), ('b', 2), ('c', 3))
>>> any(x == 'a' for x, y in seq_of_tups)
True

For tuples of any size you could use this instead:

any(x[0] == 'a' for x in seq_of_tups)

Also here are some interesting timings:

>python -m timeit -s "seq_of_tups = (('a', 1), ('b', 2), ('c', 3))" 
                 "any(x == 'a' for x, y in seq_of_tups)"
1000000 loops, best of 3: 0.564 usec per loop

>python -m timeit -s "seq_of_tups = (('a', 1), ('b', 2), ('c', 3))" 
                 "'a' in (x[0] for x in seq_of_tups)"
1000000 loops, best of 3: 0.526 usec per loop

>python -m timeit -s "seq_of_tups = (('a', 1), ('b', 2), ('c', 3)); 
                      from operator import itemgetter; from itertools import imap" 
                 "'a' in imap(itemgetter(0), seq_of_tups)"
1000000 loops, best of 3: 0.343 usec per loop
>>> tups = (('a', 1), ('b', 2), ('c', 3))

>>> 'a' in (x[0] for x in tups)
True
>>> 'd' in (x[0] for x in tups)
False

the above solution will exit as soon as a is found, proof:

>>> tups = (('a', 1),('a',5), ('b', 2), ('c', 3))
>>> gen=(x[0] for x in tups)
>>> 'a' in gen
True
>>> list(gen)
['a', 'b', 'c']  #this result means generator stopped at first 'a'

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