Counting elements in a list wildcard

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Apr 24 21:57:00 EDT 2006


"Ryan Ginstrom" <ryang at gol.com> writes:

> If there are specific spellings you want to allow, you could just
> create a list of them and see if your Suzy is in there:
> 
> >>> possible_suzys = [ 'Susy', 'Susi', 'Susie' ]
> >>> my_strings = ['Bob', 'Sally', 'Susi', 'Dick', 'Jane' ]
> >>> for line in my_strings:
> ... 	if line in possible_suzys: print line
> ... 	
> Susi

If you wanted to do something later, rather than only during the scan
over the list, getting a list of suzies would probaby be more useful:

    >>> possible_suzys = [ 'Susy', 'Susi', 'Susie' ]
    >>> my_strings = ['Bob', 'Sally', 'Susi', 'Dick', 'Susy', 'Jane' ]
    >>> found_suzys = [s for s in my_strings if s in possible_suzys]
    >>> found_suzys
    ['Susi', 'Susy']

-- 
 \        "The number of UNIX installations has grown to 10, with more |
  `\     expected."  -- Unix Programmer's Manual, 2nd Ed., 12-Jun-1972 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list