473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

partial / wildcard string match in 'in' and 'list.index()'

For a given list:

fruits=["apples","oranges","mangoes","bananas"]


Is it possible to do wildcard matches like shown below?

1. "man*" in fruits

2. fruits.index("man*")

3. "*nanas*" in fruits

4. fruits.index("*nanas")

or is there any way to achieve an equivalent effect
short of doing a while loop?
Jul 18 '05 #1
1 3468
Jon Perez wrote:
For a given list:

fruits=["apples","oranges","mangoes","bananas"]


Is it possible to do wildcard matches like shown below?

1. "man*" in fruits

2. fruits.index("man*")

3. "*nanas*" in fruits

4. fruits.index("*nanas")

or is there any way to achieve an equivalent effect
short of doing a while loop?

import fnmatch
def find(seq, pattern): .... pattern = pattern.lower()
.... for i, n in enumerate(seq):
.... if fnmatch.fnmatch(n.lower(), pattern):
.... return i
.... return -1
.... def index(seq, pattern): .... result = find(seq, pattern)
.... if result == -1:
.... raise ValueError
.... return result
.... def contains(seq, pattern): .... return find(seq, pattern) != -1
.... fruit = "apples oranges mangoes bananas".split()
contains(fruit, "man*") True contains(fruit, "*nas") True index(fruit, "*ANA*") 3 find(fruit, "*") 0 find(fruit, "m*s") 2 find(fruit, "m*x") -1


If you want case-sensitive matches, use fnmatchcase() and remove the
..lower() conversions.

Peter

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Brown | last post by:
I have researched newsgroups and the web very thoroughly and unsuccessfully for a solution to what I believe is a very common problem. I know it's easy to do wildcard match against data in DB...
12
by: Niall Porter | last post by:
Right this has to be a Micro$oft mess-up surely...? I'm running SQL 2k standard with SP3. I have a table which I'm trying to query using a LIKE operator on a varchar field as follows .......
1
by: greg.kujawa | last post by:
I am passing along an Index Server 2.0 query though URL strings calling the ASP. I am searching for records by a person's last name and their phone number. Something like: ...
2
by: Dave Smithz | last post by:
Hello there, Summary: How far can you go with SQL Select queries using like clauses with wildcard characters. Can you apply anything like regular expressions? Full details: On a Intranet...
2
by: MFRASER | last post by:
Is there a way to get a list of files that match a wildcard search? c:\temp\test.xml c:\temp\test2.xml c:\temp\test32.xml c:\temp\123test33.xml I want to get a list of c:\temp\test*.xml so...
1
by: Liu, Mingyi | last post by:
Sorry if this question has been asked before. I tried to search in postgres mailing lists at http://archives.postgresql.org/pgsql-general/ just now and it gave me error "An error occured! Can not...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
11
by: google | last post by:
I need a simple wildcard pattern matching function written in JS. I have wrestled with regular expresions but frankly am struggling to come up with anything less than an epic function of many lines...
5
by: Mr.SpOOn | last post by:
Hi, is there any way to search elements in a list using wildcards? I have a list of various elements and I need to search for elements starting with 'no', extract them and put in a new list. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.