I want to find if one word in list exists in a string and then print the found value. I am able to do this, but only with a true/false output.
JavaScript
x
5
1
mylist = ['IP', 'PR1','PR2']
2
str=('S:QUAREPControlli120111912022-06-29STL12011191-92-95-202-203_220701_PR1.g3d')
3
x = any(mylist)
4
print (x)
5
With the print (x) I print, in this case, true. But I want to print ‘PR1’. Alternatively for me is useful the index, too. How to do this? Thank you very much!
Advertisement
Answer
Try this out:
JavaScript
1
5
1
mylist = ['IP', 'PR1','PR2']
2
string = 'S:QUAREPControlli120111912022-06-29STL12011191-92-95-202-203_220701_PR1.g3d'
3
4
print([ele for ele in mylist if(ele in string)])
5