【发布时间】:2012-06-23 23:00:43
【问题描述】:
目前,我正在尝试用 Python 填充字典,但我认为我正在做的事情有点多余。有没有更 Pythonic 的方式来执行以下操作:
if not pattern_file_map.get(which_match):
pattern_file_map[which_match] = [line]
else:
pattern_file_map[which_match].append(line)
pattern_file_map 是一个字典。
我知道在检查字典中是否有键时可以使用某种习语,例如 this question,但我只想用列表填充这本字典。
【问题讨论】:
标签: python dictionary idioms