“Kamus Nested Python” Kode Jawaban

Kamus Nested Python

IDs = ['emp1','emp2','emp3']

EmpInfo = [{'name': 'Bob', 'job': 'Mgr'},
           {'name': 'Kim', 'job': 'Dev'},
           {'name': 'Sam', 'job': 'Dev'}]

D = dict(zip(IDs, EmpInfo))

print(D)
# Prints {'emp1': {'name': 'Bob', 'job': 'Mgr'},
#         'emp2': {'name': 'Kim', 'job': 'Dev'},
#         'emp3': {'name': 'Sam', 'job': 'Dev'}}
Lucky Locust

Mengakses elemen dari kamus bersarang Python

# welcome to softhunt.net
# Creating a Dictionary
Dictionary = {0: 'Softhunt', 1: '.net',
		2:{'i' : 'By', 'ii' : 'Ranjeet', 'iii' : 'Andani'}}
print('Dictionary', Dictionary)

# Accessing element using key
print(Dictionary[0])
print(Dictionary[2]['i'])
print(Dictionary[2]['ii'])
Outrageous Ostrich

Objek bersarang Python untuk dikte

def my_dict(obj):
    if not  hasattr(obj,"__dict__"):
        return obj
    result = {}
    for key, val in obj.__dict__.items():
        if key.startswith("_"):
            continue
        element = []
        if isinstance(val, list):
            for item in val:
                element.append(my_dict(item))
        else:
            element = my_dict(val)
        result[key] = element
    return result
Vivacious Vendace

Membuat kamus bersarang

# welcome to softhunt.net
# Creating a Nested Dictionary
Dictionary = {0: 'Softhunt', 1: '.net',
		2:{'i' : 'By', 'ii' : 'Ranjeet', 'iii' : 'Andani'}}
print(Dictionary)
Outrageous Ostrich

membuat dikte bersarang dari dua dikte

data = [
    [14, 77766, [2, 2]],
    [15, 77766, [1, 2]],
    [70, 88866, [1, 5]],
    [71, 88866, [2, 5]],
    [72, 88866, [5, 5]],
    [73, 88866, [4, 5]],
    [74, 88866, [3, 5]],
    [79, 99966, [1, 2]],
    [80, 99966, [2, 2]],
]

c = {}
for key, id_, (value, _) in data:
    c.setdefault(id_, {})[key] = value
print(c)
Agreeable Addax

Jawaban yang mirip dengan “Kamus Nested Python”

Pertanyaan yang mirip dengan “Kamus Nested Python”

Lebih banyak jawaban terkait untuk “Kamus Nested Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya