假设我有:
name_list = ["gxrgirl", "Octagon", "Nimble Momonga"]
feminine_words = ["girl", "princess", "wifey", "woman", "queen"]
我希望能够通过使用name_list
列表来检测feminine_words
中的女性单词。
例如:
我将能够检测到gxrgirl
中的“ girl”字符串,并显示一些输出:female
我在某处阅读了可以使用from builtins import any
的内容,但不确定如何使用它。
答案 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', '', '']