wildcard in tuples

cyberian bear cyberian_bear at hotmail.com
Sun Feb 18 03:50:47 CET 2001


Yep, that looks like just what i needed. but is there a way to modify those
statements so that they can be used in 'if then ' statement. So it will go
something like that:
if (# of matched terms is greater than 0) :
    do stuff

thanx
"Erik Max Francis" <max at alcyone.com> wrote in message
news:3A8F1F7D.6781AF14 at alcyone.com...
> 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.





More information about the Python-list mailing list