dict 字典 key-value 变数

Python 基础: dict 字典 key-value 变数

一般 dict

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}
print("user_dict: ", user_dict)
print("user.name: ", user_dict['name'])
print("user.home: ", user_dict['home'])
print("user.job: ", user_dict['job'])

# user_dict:  {'name': 'KJ', 'home': 'Taiwan', 'job': 'Developer'}
# user.name:  KJ
# user.home:  Taiwan
# user.job:  Developer

变更 dict

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

user_dict['name'] = 'Kay'
print('user_dict: ', user_dict)

# user_dict:  {'name': 'Kay', 'home': 'Taiwan', 'job': 'Developer'}

in 键值是否存在

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

if 'name' in user_dict:
    print('key "name" exist')
else:
    print('key "name" not exist')

if 'age' in user_dict:
    print('key "age" exist')
else:
    print('key "age" not exist')

# key "name" exist
# key "age" not exist

not in 键值是否不存在

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

if 'name' not in user_dict:
    print('key "name" not exist')
else:
    print('key "name" exist')

if 'age' not in user_dict:
    print('key "age" not exist')
else:
    print('key "age" exist')

# key "name" exist
# key "age" not exist

del 删除指定键值

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}
print("user_dict: ", user_dict)
del user_dict['job']
print("user_dict: ", user_dict)

# user_dict:  {'name': 'KJ', 'home': 'Taiwan', 'job': 'Developer'}
# user_dict:  {'name': 'KJ', 'home': 'Taiwan'}

dict.clear() 删除所有键值资料

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}
print("user_dict: ", user_dict)
user_dict.clear()
print("user_dict: ", user_dict)

# user_dict:  {'name': 'KJ', 'home': 'Taiwan', 'job': 'Developer'}
# user_dict:  {}

dict.values() 取得所有资料

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

user_dict_value = user_dict.values()
print('user_dict_value:', user_dict_value)
# user_dict_value: dict_values(['KJ', 'Taiwan', 'Developer'])

user_dict.keys() 取得所有键值

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

user_dict_keys = user_dict.keys()
print('user_dict_keys: ', user_dict_keys)
# user_dict_keys:  dict_keys(['name', 'home', 'job'])

dict.items() 取得资料及键值项目资讯

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

user_dict_items = user_dict.items()
print(user_dict_items)
# dict_items([('name', 'KJ'), ('home', 'Taiwan'), ('job', 'Developer')])

for in 迴圈: 用 key 取值

for key in user_dict:
    print('key => value: ', key, user_dict[key])

# key => value:  name KJ
# key => value:  home Taiwan
# key => value:  job Developer

for in 迴圈: dict.values() 数值

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

for value in user_dict.values():
    print('value: ', value)

# value:  KJ
# value:  Taiwan
# value:  Developer

for in 迴圈: dict.values() 键值

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

for key in user_dict.keys():
    print('key: ', key)

# key:  name
# key:  home
# key:  job

for in 迴圈: dict.items() 资料

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

for key, value in user_dict.items():
    print("key => value: ", key, value)

# key => value:  name KJ
# key => value:  home Taiwan
# key => value:  job Developer

dict.copy() 複製字典

user_dict = {
    "name": "KJ",
    "home": "Taiwan",
    "job": "Developer"
}

copy_user_dict = user_dict.copy()
print('user_dict: ', user_dict)
print('copy_user_dict: ', copy_user_dict)

# user_dict:  {'name': 'KJ', 'home': 'Taiwan', 'job': 'Developer'}
# copy_user_dict:  {'name': 'KJ', 'home': 'Taiwan', 'job': 'Developer'}

巢状字典

user_dict = {
    "developer1": {
        "name": "KJ",
        "home": "Taiwan",
        "job": "Developer"
    },
    "developer2": {
        "name": "Kay",
        "home": "Taipei",
        "job": "Frontend"
    },
    "developer3": {
        "name": "Jay",
        "home": "Taipei",
        "job": "Backend"
    }
}

print('user_dict: ', user_dict)
# user_dict:  {'developer1': {'name': 'KJ', 'home': 'Taiwan', 'job': 'Developer'}, 'developer2': {'name': 'Kay', 'home': 'Taipei', 'job': 'Frontend'}, 'developer3': {'name': 'Jay', 'home': 'Taipei', 'job': 'Backend'}}

变更巢状字典

user_dict = {
    "developer1": {
        "name": "KJ",
        "home": "Taiwan",
        "job": "Developer"
    },
    "developer2": {
        "name": "Kay",
        "home": "Taipei",
        "job": "Frontend"
    },
    "developer3": {
        "name": "Jay",
        "home": "Taipei",
        "job": "Backend"
    }
}

user_dict['developer3']['name'] = 'John'
print('user_dict: ', user_dict)

参考资料