简体   繁体   English

查找字符串是否包含列表中的单词之一并打印它找到的单词

[英]Find if string contains one of the word in list and print the word it finds

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.我能够做到这一点,但只能使用真/假输出。

mylist = ['IP', 'PR1','PR2']
str=('S:\QUAREP\Controlli\12011191\2022-06-29\STL\12011191-92-95-202-203_220701_PR1.g3d')
x = any(mylist)
print (x)

With the print (x) I print, in this case, true.使用 print (x) 我打印,在这种情况下,是真的。 But I want to print 'PR1'.但我想打印'PR1'。 Alternatively for me is useful the index, too.或者对我来说,索引也很有用。 How to do this?这个怎么做? Thank you very much!非常感谢!

Try this out:试试这个:

mylist = ['IP', 'PR1','PR2']
string = 'S:\QUAREP\Controlli\12011191\2022-06-29\STL\12011191-92-95-202-203_220701_PR1.g3d'

print([ele for ele in mylist if(ele in string)])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查字符串是否包含一个单词,该单词是列表中单词的子集 - Checking if string contains a word that is subset of a word in a list 以单词格式打印python列表,例如一个字符串 - Print a python list in a word format like is one string 字符串三角形模式未打印正确的 output(当字符串包含多个单词时) - String triangle pattern does not print the correct output (when the string contains more than one word) 在另一个列表中查找包含 5 个连续字符串的单词 - Find a word that contains 5 consecutive strings in another list 在 C 文件中查找包含单词的字符串定义 - Find string definitions that contains a word in C file Function 接受一串字母,从有效单词列表中输出单词列表,然后找到拼字游戏得分最高的单词 - Function that takes a string of letters, outputs a list of words from a Valid Word List then finds the word with the highest scrabble score Python - 如果string包含列表或集合中的单词 - Python - If string contains a word from a list or set 在由xlrd创建的列表中查找单词并打印句子 - Find word in list created by xlrd and print sentence 打印并查找单词和单词在列表中的位置 - Print and Find The Position Of A Word And Words In A List 快速查找单词列表是否包含至少一个以某些字母开头的单词(不是“查找所有单词”!) - Fast way to find if list of words contains at least one word that starts with certain letters (not "find ALL words"!)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM