【发布时间】:2011-03-17 08:02:26
【问题描述】:
我可以在 python 中做这样的事情:
l = ['one', 'two', 'three']
if 'some word' in l:
...
这将检查列表中是否存在“某个单词”。但是我可以做相反的事情吗?
l = ['one', 'two', 'three']
if l in 'some one long two phrase three':
...
我必须检查数组中的某些单词是否在字符串中。我可以使用循环来做到这一点,但这种方式有更多的代码行。
【问题讨论】:
-
使用“list”作为变量名会给你带来麻烦,因为它隐藏了python“list”
-
@Fabian,是的,当然。仅供参考。
-
优先考虑可读性优于代码行数。在这种情况下,
any是一个完美的选择,但有时使用更多的线条比一个衬里更好