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)

參考資料