【发布时间】:2021-02-04 04:48:51
【问题描述】:
我有一本长字典,看起来像这样:
name = 'Barack.'
name_last = 'Obama!'
street_name = "President Streeet?"
list_of_slot_names = {'name':name, 'name_last':name_last, 'street_name':street_name}
我想删除每个插槽的标点(名称、名称_last、...)。
我可以这样做:
name = name.translate(str.maketrans('', '', string.punctuation))
name_last = name_last.translate(str.maketrans('', '', string.punctuation))
street_name = street_name.translate(str.maketrans('', '', string.punctuation))
你知道更短(更紧凑)的写法吗?
结果:
>>> print(name, name_last, street_name)
>>> Barack Obama President Streeet
【问题讨论】:
-
list_of_slot_names是一个令人困惑的 dict 名称,请考虑dict_of_slot_names
标签: python string loops dictionary punctuation