-1

I tried searching around , perhaps I am not getting exact way to search the web or stack overflow for this requirement. Kindly re-direct me or provide a solution for one's below.

I have a list say (My original list has over 10k cities) :

A = ['BANGALORE', 'DUBAI', 'SHANGHAI' , 'SOUTH ASIA' , 'NORTH ASIA']

Now my requirement is match this list similar to LIKE operator in ORACLE. I mean , if we enter BAN , it has to match BANGALORE. I accomplish this by following :

if 'BAN'in  str(A) :
   Do my stuffs  

Now , if I need to retrieve that element from list where 'BAN' matched . How can we do it ?

In simple terms .

if 'BAN'in  str(A) :
   Display to me what is that A that matched 'BAN'. 

I did some searching around but couldn't find a a solution

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
San
  • 161
  • 3
  • 13
  • well , i will need that value of A where BAN matched ! I have over 10k cities in the list. I know i am matching BAN , but i do not know on what A is that matching. I want to find that ! – San Mar 01 '17 at 13:02
  • Possible duplicate of [python: finding substring within a list](http://stackoverflow.com/questions/13779526/python-finding-substring-within-a-list) – Chris_Rands Mar 01 '17 at 13:12
  • Agree , That's what i have put in my post. I tried my ways to search but could not find one probably because of the wordings i used during search ! – San Mar 02 '17 at 02:05

1 Answers1

2
[city for city in A if 'BAN' in city]

This gives you a list with every city that contains 'BAN'. You can check if the list is empty and then print it or take the first element, or whatever you want to do.

matching = [city for city in A if 'BAN' in city]
if matching:
  print 'There are ', len(matching), ' cities that contain BAN.'
  print 'For example: ', matching[0]
Sorin
  • 11,863
  • 22
  • 26
  • I shall try out this solution before accepting this answer. Thank you very much :) – San Mar 01 '17 at 13:07