我想检查一个包含一些关键字的列表。
我的写作如下:
if ('keyword1' or 'keyword2' or 'keyword3') in [list]:
以上作品,但似乎写得不好。
还有其他改进方法吗?
答案 0 :(得分:0)
您可以使用python内置search_terms = ['a', 'keyword1']
list = ['blabla', 'keyword1', 'keyword4']
if any(word in list for word in search_terms):
// TRUE
方法,https://docs.python.org/2/library/functions.html#any
>>> l = [2.4, 4.8532, 5.43253]
>>> l2 = [12.4, 374.8532, -505.43253]
>>> list(map(int, (i[0] for i in map(str, l))))
[2, 4, 5]
>>> list(map(int, l))
[2, 4, 5]
>>> list(map(int, (i[0] for i in map(str, l2))))
>>> [i[0] for i in map(str, l2)]
['1', '3', '-']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '-'
>>> list(map(int, l2))
[12, 374, -505]