【发布时间】:2019-10-01 02:28:04
【问题描述】:
我有一个列表e
e = ['s', 'mm', 'ng']
还有一个字典d
d = {'A1': ['Tomas',
'john',
'2s',
'Douglas',
'20ng'],
'B1': ['Tomm',
'3mm',
'Sterling',
'hey']}
我的目标是只跳过d 中以数字和e 中的元素结尾的名称。
例如,d 中的 2s 将被跳过,因为它有一个数字 2 和一个来自列表 e 的元素 s。
我已经尝试了以下
r = {}
for k, v in d.items():
r[k] = [s for s in v if not any(s.endswith(val) for val in e)]
我得到了
{'A1': ['john'], 'B1': ['hey']}
我的代码是删除以s 结尾的元素,例如'Tomas'
我想要的输出是以下内容,其中仅删除了 e 中的数字 + 元素,例如 3mm
{'A1': ['Tomas', 'john', 'Douglas'], 'B1': ['Tomm', 'Sterling', 'hey']}
如何更改代码以获得所需的输出?
【问题讨论】:
标签: regex python-3.x list dictionary text