在使用另一个列表作为参考python的同时在列表中查找字符串

时间:2018-09-05 03:52:34

标签: python string substring

假设我有:

name_list = ["gxrgirl", "Octagon", "Nimble Momonga"]
feminine_words = ["girl", "princess", "wifey", "woman", "queen"]

我希望能够通过使用name_list列表来检测feminine_words中的女性单词。

例如:

我将能够检测到gxrgirl中的“ girl”字符串,并显示一些输出:female

我在某处阅读了可以使用from builtins import any的内容,但不确定如何使用它。

1 个答案:

答案 0 :(得分:0)

您可以使用函数any(),而无需使用任何import语句。

可以用一行解决:

['female' if any(word in name for word in feminine_words) else '' for name in name_list]
# It gives the following result: ['female', '', '']