473,396 Members | 1,755 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,396 software developers and data experts.

search substring in list of strings

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+051E","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 incomprehensible 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 21785
bvdet
2,851 Expert Mod 2GB
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+051E","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 incomprehensible 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 Expert 256MB
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+051E","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
Thanks a bunch, guys!
Apr 9 '07 #4

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

Similar topics

10
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:...
10
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...
1
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
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...
1
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...
4
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...
0
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...
9
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...
14
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...
0
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...
0
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,...

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.