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
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
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!
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]
I tried with something like
Code:
if x.find(hdrs[image]['some keyword']) !=-1 for x in refdict.keys():
any help will be greatly appreciated!
Comment