473,803 Members | 3,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

search substring in list of strings

15 New Member
Hello.

Is there an easy one-line way to see if a list of strings contains at least one occurance of a substring? E.g.

string_list=["PG2213-006B","PG0231+0 51E","PG2213-006A"]

substring_list=["PG2213-006","PG0231"]


I really don't want to loop, as I loop too many times in my code already.

Here's the way it is now:

images is a list of image names

refdict contains full names as keys

hdrs contains some of those keys but truncated

Expand|Select|Wrap|Line Numbers
  1. for image in images:
  2. for x in refdict.keys():
     if x.find(hdrs[image]['some keyword']) != -1:
     print "Already have that key, skipping image."
but this way I have to either continue with the checking of the substring even after I've found it once or insert a break statement which means another if-control statement which means more incomprehensibl e code.

I tried with something like

Expand|Select|Wrap|Line Numbers
  1. if x.find(hdrs[image]['some keyword']) !=-1 for x in refdict.keys():
  2.  
but that naturally didn't want to work for me. Is there a way of using has_key but only make it find a substring of the key instead of the whole key?


any help will be greatly appreciated!
Apr 9 '07 #1
3 21801
bvdet
2,851 Recognized Expert Moderator Specialist
Hello.

Is there an easy one-line way to see if a list of strings contains at least one occurance of a substring? E.g.

string_list=["PG2213-006B","PG0231+0 51E","PG2213-006A"]

substring_list=["PG2213-006","PG0231"]


I really don't want to loop, as I loop too many times in my code already.

Here's the way it is now:

images is a list of image names

refdict contains full names as keys

hdrs contains some of those keys but truncated

Expand|Select|Wrap|Line Numbers
  1. for image in images:
  2. for x in refdict.keys():
     if x.find(hdrs[image]['some keyword']) != -1:
     print "Already have that key, skipping image."
but this way I have to either continue with the checking of the substring even after I've found it once or insert a break statement which means another if-control statement which means more incomprehensibl e code.

I tried with something like

Expand|Select|Wrap|Line Numbers
  1. if x.find(hdrs[image]['some keyword']) !=-1 for x in refdict.keys():
  2.  
but that naturally didn't want to work for me. Is there a way of using has_key but only make it find a substring of the key instead of the whole key?


any help will be greatly appreciated!
Maybe you can use this:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. string_list=["PG2213-006B","PG0231+051E","PG2213-006A"]
  4.  
  5. substring_list=["PG2213-005","PG0231","D0000","PG2213-006"]
  6.  
  7. patt = r'%s' % '|'.join(substring_list)
  8. print [re.match(patt, i) for i in string_list]
  9.  
  10. if None not in [re.match(patt, i) for i in string_list]:
  11.     print 'Perform some action'
  12. else:
  13.     print 'No match'
  14.  
  15. '''
  16. >>> [<_sre.SRE_Match object at 0x00E1A4B8>, <_sre.SRE_Match object at 0x00E1A4F0>, <_sre.SRE_Match object at 0x00E1A528>]
  17. Perform some action
  18. >>> 
  19. '''
Apr 9 '07 #2
ghostdog74
511 Recognized Expert Contributor
Hello.

Is there an easy one-line way to see if a list of strings contains at least one occurance of a substring? E.g.

string_list=["PG2213-006B","PG0231+0 51E","PG2213-006A"]

substring_list=["PG2213-006","PG0231"]
using simple string methods
Expand|Select|Wrap|Line Numbers
  1. >>> string_list=' '.join(["PG2213-006B","PG0231+051E","PG2213-006A"])
  2. >>> substring_list=["PG2213-006","PG0231"]
  3. >>> for i in substring_list:
  4. ...  if i in string_list:
  5. ...   print "substring ", i , "found"
  6. ...
  7. substring  PG2213-006 found
  8. substring  PG0231 found
  9. >>>                                  
  10.  
Apr 9 '07 #3
eso40043
15 New Member
Thanks a bunch, guys!
Apr 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
39359
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1: domything() And the regexp search assuming no case restriction would be,
10
3894
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no more than 10 letters, and find all possible mutations that match a word in my dictionary (80k words). However a wildcard letter '?' is also an acceptable character which increases the worst case time significantly. So if the letters are check...
1
3175
by: ajay.sonawane | last post by:
How can I find the wheather the occurance substring is main string in wide char provided that the compasion should be case insensitive.
32
14915
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
1
3072
by: Nico Grubert | last post by:
Hi there, I would like to search for a substring in a string and get the index of all occurances. mystring = 'John has a really nice powerbook.' substr = ' ' # space I would like to get this list:
4
3575
by: gwtc | last post by:
Here is a google search site bookmarklet. This lets you search a certain website using google. What I want is the same thing, but to search a certain geocities site. When you use the current bookmarklet, the search takes you to site:www.geocities.com instead of the specific site you want to search. Can someone modify this for me, to search specific geocities sites? Thanks for your help and btw I'm using Mozilla Suite and Firefox. ...
0
1149
by: deeak | last post by:
Hi All, I have set of strings in the db2 table and two range keys to search. I have to finds strings the table which would be in between the range of keys defined. Strings in the colum to search (COLUMN_TO_SEARCH) : KAVICEH&,KAVICEH>,K$VICEH&,KAVICEH$ Range of keys defined : KAVIC$$$ , KAVIC$$/ SQL query may looks like :
9
2532
by: tinnews | last post by:
What's the neatest and/or most efficient way of testing if one of a set of strings (contained in a dictionary, list or similar) is a sub-string of a given string? I.e. I have a string delivered into my program and I want to see if any of a set of strings is a substring of the string I have been given. It's quite OK to stop at the first one found. Ideally the strings being searched through will be the keys of a dictionary but this...
14
2982
by: S | last post by:
Any idea on how I would be able to do a search within C# that does ranges or words For example I want to search for Chicken in the string string s1 = "This is Great Chicken";
0
9704
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...
1
10302
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
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
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
7608
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
6844
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
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
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
3
2975
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.