473,666 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

For a given list:

fruits=["apples","orang es","mangoes"," bananas"]


Is it possible to do wildcard matches like shown below?

1. "man*" in fruits

2. fruits.index("m an*")

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 3479
Jon Perez wrote:
For a given list:

fruits=["apples","orang es","mangoes"," bananas"]


Is it possible to do wildcard matches like shown below?

1. "man*" in fruits

2. fruits.index("m an*")

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
7419
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 (using LIKE and "%" and "?"). But is it possible to match a concrete string against a database of wildcarded data? ("%" and LIKE do not work). For example: CREATE TABLE blacklist (
12
10265
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 .... WHERE dbo.tbl_pm_projects.SeniorManagerID LIKE '%' .... In actual fact the % is passed in by the application when the user
1
2245
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: http://myserver/query.asp?ciRestriction="lastname"%20and%20"555-1212" Here is the chunk of code that processes the query request: ciRestriction = Request.QueryString("ciRestriction") Set oQuery = Server.CreateObject("IXSSO.Query")
2
13357
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 website, I request a code from a user which is then compared with a code in an ADO access database.
2
2278
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 I should get a collection of three file items
1
3476
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 connect to search daemon". Anyway I installed tsearch2 and openFTS and was able to do some searches. However, when I checked searching partial words, it of course does not work(unless the partial word just happens to be the indexed version). IMHO,...
78
4582
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 excludes '*a*b*' and '*c*d*a*'. See below for details. CHALLENGE: generate an equivalent in ruby, lisp, haskell, ocaml, or in a CAS like maple or mathematica. #------------------------------------------------------------------------------- # Short...
11
22323
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 of code ... Any help much appreciated! Sample string : "the cat sat on the mat" matches : "the cat*" , "*cat*" , "*the mat"
5
18691
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 was thinking about something like: mylist.index('no*') Of course this doesn't work.
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.