HashCode
HashCode

Reputation: 51

Search multiple strings

I have two lists of strings - x and y. x is the header from different web pages (are delimited by hyphen):

x = [  
     'ABC-12345-TTTT-MMMM-00000-FINSA1',
     'ABC-0000-KKKK-MMMM-00000-FINSA2' ,
     'ABC-3987-TKTKT-JJJJ-00000-FINSA1',
     'ABC-9990-TTTT-MMMM-00000-FINSA4' ,
     'ABC-5990-NNNN-MUUUT-00000-FINSA2'
     ]

y is the carrier code:

y = ['FINSA1','FINSA2']

I want to match all strings in y (for all carrier codes) to all strings in x (all Headers). If match is found, then append '-X1' at the end of each matched x.

The append part is easy, but I am not able to match each string in y.

I tried using the following:

  1. if (x.find(y) != -1): (this only returns the last value FINSA2)
  2. name = [1 for i in x if subs in y] - doesn't work

Upvotes: 0

Views: 43

Answers (1)

Roland Smith
Roland Smith

Reputation: 43495

A combination of any in a conditional expression in a list comprehension should to the trick:

In [1]: x = [   
   ...:      'ABC-12345-TTTT-MMMM-00000-FINSA1', 
   ...:      'ABC-0000-KKKK-MMMM-00000-FINSA2' , 
   ...:      'ABC-3987-TKTKT-JJJJ-00000-FINSA1', 
   ...:      'ABC-9990-TTTT-MMMM-00000-FINSA4' , 
   ...:      'ABC-5990-NNNN-MUUUT-00000-FINSA2' 
   ...:      ] 
   ...: 

In [2]: y = ['FINSA1','FINSA2']

In [3]: [i+'-X1' if any(t in i for t in y) else i for i in x]
Out[3]: 
['ABC-12345-TTTT-MMMM-00000-FINSA1-X1',
 'ABC-0000-KKKK-MMMM-00000-FINSA2-X1',
 'ABC-3987-TKTKT-JJJJ-00000-FINSA1-X1',
 'ABC-9990-TTTT-MMMM-00000-FINSA4',
 'ABC-5990-NNNN-MUUUT-00000-FINSA2-X1']

Let's break this down. The outer expression is a list comprehension over x, with i representing individual elements of x. t in i for t in y is a generator expression, which is a kind of iterable. The any function takes an iterable and produces True if any of the values in the iterator are true. So if any of the strings in y are in i, any returns True. In that case, -X1 is appended to i.

Upvotes: 1

Related Questions