将列表的字符串开头与白名单进行比较

Compare beginning of strings of a list with a whitelist

我想比较一个列表和一个白名单。我现在做的是:

>>> whitelist = ("one", "two")
>>> my_list = ["one", "two foo", "two bar", "three"]
>>> for item in my_list:
...     if not item.startswith(whitelist):
...         print(item)
three

有没有更有效/"better"的方法?

print '\n'.join([item for item in my_list if not item.startswith(whitelist)])

您可以使用列表理解:

>>> [item for item in my_list if not item.startswith(whitelist)]
['three']