search substring in list of strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eso40043
    New Member
    • Feb 2007
    • 15

    #1

    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+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

    Code:
    for image in images:
    [INDENT]for x in refdict.keys():[INDENT] if x.find(hdrs[image]['some keyword']) != -1:[INDENT] print "Already have that key, skipping image."[/INDENT][/INDENT]
    [/INDENT]
    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

    Code:
    if x.find(hdrs[image]['some keyword']) !=-1 for x in refdict.keys():
    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!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by eso40043
    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

    Code:
    for image in images:
    [INDENT]for x in refdict.keys():[INDENT] if x.find(hdrs[image]['some keyword']) != -1:[INDENT] print "Already have that key, skipping image."[/INDENT][/INDENT]
    [/INDENT]
    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

    Code:
    if x.find(hdrs[image]['some keyword']) !=-1 for x in refdict.keys():
    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:
    Code:
    import re
    
    string_list=["PG2213-006B","PG0231+051E","PG2213-006A"]
    
    substring_list=["PG2213-005","PG0231","D0000","PG2213-006"]
    
    patt = r'%s' % '|'.join(substring_list)
    print [re.match(patt, i) for i in string_list]
    
    if None not in [re.match(patt, i) for i in string_list]:
        print 'Perform some action'
    else:
        print 'No match'
    
    '''
    >>> [<_sre.SRE_Match object at 0x00E1A4B8>, <_sre.SRE_Match object at 0x00E1A4F0>, <_sre.SRE_Match object at 0x00E1A528>]
    Perform some action
    >>> 
    '''

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by eso40043
      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
      Code:
      >>> string_list=' '.join(["PG2213-006B","PG0231+051E","PG2213-006A"])
      >>> substring_list=["PG2213-006","PG0231"]
      >>> for i in substring_list:
      ...  if i in string_list:
      ...   print "substring ", i , "found"
      ...
      substring  PG2213-006 found
      substring  PG0231 found
      >>>

      Comment

      • eso40043
        New Member
        • Feb 2007
        • 15

        #4
        Thanks a bunch, guys!

        Comment

        Working...