我在python中创建字典:d = dict.fromkeys(['one', 'two', 'three'], 1)
但是我不知道如何将多个键添加到该字典中,例如 ['one2','two2','three2']-> 2
因此,结果字典应为:
['一个','两个','三个']-> 1 ['one2','two2','three2']-> 2
答案 0 :(得分:0)
一种方法是使用另一本词典的结果进行更新:
dict.fromkeys
结果:
d = dict.fromkeys(['one', 'two', 'three'], 1)
d.update(dict.fromkeys( ['one2', 'two2', 'three2'] , 2))
如果您有更多数据要更新,可能会很乏味。在这种情况下,用键/值创建一个元组列表,然后使用dict理解将其“展平”:
{'one': 1, 'two': 1, 'two2': 2, 'three2': 2, 'three': 1, 'one2': 2}