How to construct a defaultdict from a dictionary?

后端 未结 3 526
挽巷
挽巷 2021-02-06 19:57

If I have d=dict(zip(range(1,10),range(50,61))) how can I build a collections.defaultdict out of the dict?

The only argument

3条回答
  •  我在风中等你
    2021-02-06 20:51

    You can construct a defaultdict from dict, by passing the dict as the second argument.

    from collections import defaultdict
    
    d1 = {'foo': 17}
    d2 = defaultdict(int, d1)
    
    print(d2['foo'])  ## should print 17
    print(d2['bar'])  ## should print 1 (default int val )
    

提交回复
热议问题