How to construct a defaultdict from a dictionary?

后端 未结 3 527
挽巷
挽巷 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条回答
  •  -上瘾入骨i
    2021-02-06 20:36

    Read the docs:

    The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

    from collections import defaultdict
    d=defaultdict(int, zip(range(1,10),range(50,61)))
    

    Or given a dictionary d:

    from collections import defaultdict
    d=dict(zip(range(1,10),range(50,61)))
    my_default_dict = defaultdict(int,d)
    

提交回复
热议问题