我有元组列表:
tupl = [('0', 'Hey'),('1', ','),('2', 'I'), ('3', 'feel'),('4', 'you'), ('5', '!')]
我想删除任何包含标点符号的元组。
我已经尝试使用以下代码,但是它对起作用'!'
,仅因为我不知道如何在此代码中放置多个条件。
out_tup = [i for i in tupl if '!' not in i]
print out_tup
如何删除所有包含标点符号的元组(例如','
)?
使用 any
例如:
import string
tupl = [('0', 'Hey'),('1', ','),('2', 'I'), ('3', 'feel'),('4', 'you'), ('5', '!')]
print([i for i in tupl if not any(p in i for p in string.punctuation)])
#or
print([i for i in tupl if not any(p in i for p in [",", "!"])])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句