wildcard in tuples

Erik Max Francis max at alcyone.com
Sun Feb 18 02:03:57 CET 2001

```cyberian bear wrote:

> lets say i have a tuple in one list and i need to check it against
> every
> tuple in second list.  So (e,c) --and i want to find every tuple in
> second
> list which has tuples who are (e, *). What is the proper syntax for
> doing
> this

I'm not sure exactly what you're asking.  If all you want is to select
all 2-tuples that, say, have 2 as the first element, then you can do it
easily enough with list comprehensions:

>>> l = [(1, 1), (2, 3), (2, 4), (3, 5), (3, 6), (4, 4)]
>>> [ x for x in l if x[0] == 2 ]
[(2, 3), (2, 4)]

This can obviously be trivially modified for matching on the second
list.

You could do something similar using None as a wildcard:

>>> def selectTuple(list, tuple):
...     return [ x for x in list if (x[0] == tuple[0] or tuple[0] is \
...              None) and (x[1] == tuple[1] or tuple[1] is None) ]
...
>>> l = [(1, 1), (2, 3), (2, 4), (3, 5), (3, 6), (4, 4)]
>>> selectTuple(l, (2, 3))
[(2, 3)]
>>> selectTuple(l, (2, None)) # tuples beginning with 2
[(2, 3), (2, 4)]
>>> selectTuple(l, (None, 4)) # tuples ending with 4
[(2, 4), (4, 4)]
>>> selectTuple(l, (None, None)) # all tuples
[(1, 1), (2, 3), (2, 4), (3, 5), (3, 6), (4, 4)]

--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Society attacks early when the individual is helpless.
\__/ B.F. Skinner
Physics reference / http://www.alcyone.com/max/reference/physics/
A physics reference.

```