“DefaultDict Python Dict Inside Dict” Kode Jawaban

Defaultdict Python untuk Dikt

>>> #You can simply call dict:
>>> a
defaultdict(<type 'list'>, {'1': ['b', 'a'], '3': ['b'], '2': ['a']})
>>> dict(a)
{'1': ['b', 'a'], '3': ['b'], '2': ['a']}

# but remember that a defaultdict is a dict
# (with some special behavior, check source):
>>> isinstance(a, dict)
True
Index out of bounds

DefaultDict Python Dict Inside Dict

nums = [1, 2, 1, 1, 4, -4, 2, 2, 4, 2, 5, 7]
dict_in_dict = defaultdict(dict)
for i in range(0, len(nums), 3):
    dict_in_dict[nums[i+1]][nums[i]] = -1*nums[i+2]
dict(dict_in_dict)

# result
# {2: {1: -1, 2: -4}, 4: {1: 4}, 5: {2: -7}}
Adventurous Addax

Jawaban yang mirip dengan “DefaultDict Python Dict Inside Dict”

Pertanyaan yang mirip dengan “DefaultDict Python Dict Inside Dict”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya