字典定义
- 字典中的数据是以键值对 (Key-Value) 的方式存储。
- 字典中的键必须是唯一的,类似于json对象格式。
字典基本操作
d = {key1: value1, key2: value2, key3: value3 }
d = {"Country": "China", "City": "Beijing"}
print(d["Country"])
d["Country"] = "America"
print(d)
d["Food"] = "KaoYa"
print(d)
d.pop("Food")
print(d)
字典遍历
for key in d.keys():
print(key, end=" ")
for value in d.values():
print(value, end=" ")
print(list(d.keys()))
print(list(d.values()))
print(list(d.items()))
for key, value in d.items():
print(f'country: {key} city: {value}')
print("{0} -- {1}".format(key, value))