花括号 {} 括起来的,以逗号分隔的 值对 形式的列表 - comma-separated list of key: value pairs within
braces
# 空字典
d = {}
# 三个元素的字典
d = {'name': 'glpla', 'age': 18, 'gender': true}
字典推导式 - dict comprehension;同列表 list、set,除了值 value 外,还要生成键 key
# {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
d = {x: x for x in range(10)}
类型构造器 - type constructor: dict()
d = dict([("name", "glpla"), ("age", 18), ("gender", True)])
d = dict(name="glpla", age=18, gender=True)
d = dict(zip(["name", "age", "gender"], ["glpla", 18, True]))
字符串
d = {'name': 'glpla', 'age': 18, 'gender': true}
tuple为key
d = {('name','age'):'123'}
list为key,报错:TypeError: unhashable type: 'list'
d = {['name','age']:'123'}
使用变量:a=12
d = {('name','age'):'123',a:20}