python3 字典(dict)基礎


1、定義及初始化

1.1、直接初始化

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print(info)

  打印輸出結果:

{'stu1101': 'jack', 'stu1102': 'tom', 'stu1103': 'mary'}

1.2、先定義后賦值

records ={}
records["stu1101"] = 90
records["stu1102"] = 80
records["stu1103"] = 70
print(records)

  打印輸出結果:

{'stu1101': 90, 'stu1102': 80, 'stu1103': 70}

2、方法

2.1、setdefault()

  設置默認值。

2.2.1、對新的key設置默認值

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
info.setdefault("stu1100", "robin")
print(info)

  打印輸出結果:

{'stu1101': 'jack', 'stu1102': 'tom', 'stu1103': 'mary', 'stu1100': 'robin'}

2.2.2、對已存在的key設置默認值,原來的值不改變

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
info.setdefault("stu1101", "robin")
print(info)

  打印輸出結果:

{'stu1101': 'jack', 'stu1102': 'tom', 'stu1103': 'mary'}

2.3、keys()與values()

2.3.1、keys()

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print(info.keys())
print(type(info.keys()))

  打印輸出結果:

dict_keys(['stu1101', 'stu1102', 'stu1103'])
<class 'dict_keys'>

2.3.2、讀取keys()的元素

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
info_keys_list = list(info.keys())
print(info_keys_list[0])
print(info_keys_list[1])
print(info_keys_list[2])

  打印輸出結果:

stu1101
stu1102
stu1103

 

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
for key in info.keys():
    print(key)

  打印輸出結果:

stu1101
stu1102
stu1103

2.3.3、values()

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print(info.values())
print(type(info.values()))

  打印輸出結果:

dict_values(['jack', 'tom', 'mary'])
<class 'dict_values'>

2.4、items()

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print(info.items())
for k,v in info.items():
    print("%s: %s"%(k,v))

  打印輸出結果:

dict_items([('stu1101', 'jack'), ('stu1102', 'tom'), ('stu1103', 'mary')])
stu1101: jack
stu1102: tom
stu1103: mary

2.5、get()

  獲取key對應的鍵值。

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print(info.get('stu1103'))
print(info.get('stu1111',None))

  打印輸出結果:

mary
None

2.6、字典中是否有某個key,info默認取keys()

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print("stu2200" in info)
print("stu1101" in info)
# 等同於上面的寫法
print("stu1101" in info.keys())

  打印輸出結果:

False
True
True

2.7、字典中是否有某個value

# info默認去keys,因此這用用法是錯誤的
print("tom" in info)
# 直接去info的values(),再判斷"tom"是否在其中
print("tom" in info.values())

  打印輸出結果:

False
True

2.8、update()

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
b = {
    "stu1101": "alex",
    "stu1104": "white",
    "stu1105": "black",
}
info.update(b)
print(info)

  打印輸出結果:

{'stu1101': 'alex', 'stu1102': 'tom', 'stu1103': 'mary', 'stu1104': 'white', 'stu1105': 'black'}

2.9、del

2.9.1、刪除字典中的鍵

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
del info["stu1101"]
print(info)

  打印輸出結果:

{'stu1102': 'tom', 'stu1103': 'mary'}

2.9.2、刪除字典

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
del info
print(info)

  打印輸出結果:

name 'info' is not defined

2.10、pop()

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print(info.pop("stu1101"))

  打印輸出結果:

jack

2.11、popitem()

   隨機返回並刪除字典中的一對鍵和值(一般刪除末尾對)。

info = {
    "stu1101": "jack",
    "stu1102": "tom",
    "stu1103": "mary",
}
print(info.popitem())
print(info)

  打印輸出結果:

('stu1103', 'mary')
{'stu1101': 'jack', 'stu1102': 'tom'}

2.12、fromkeys()

  創建一個新字典,以序列seq中元素做字典的鍵,value為字典所有鍵對應的初始值。

key_list = ["name","age","height"]
dic = dict.fromkeys(key_list,None)
print(dic)

  打印輸出結果:

'name': None, 'age': None, 'height': None}

2.13、sorted()

  所有可迭代的對象進行排序操作  

  sort 與 sorted 區別:

    sort 是應用在 list 上的方法,sorted 可以對所有可迭代的對象進行排序操作。

    list 的 sort 方法返回的是對已經存在的列表進行操作,而內建函數 sorted 方法返回的是一個新的 list,而不是在原來的基礎上進行的操作。

a = {6:2,8:0,1:4,-5:6,99:11,4:22}
print(  sorted(a.items()) )
print(  sorted(a.items(),key=lambda x:x[1]) )
print(  sorted(a.items(),key=lambda x:x[0]) )

  打印輸出結果:

[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
[(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]
[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]

2.14、zip()

  用於將可迭代的對象作為參數,將對象中對應的元素打包成一個個元組,然后返回由這些元組組成的列表。

a = [1,2,3,4,5,6]
b = ['a','b','c','d']

for i in zip(a,b):
    print(type(i))
    print(i)
print("*"*50)
print(list(zip(a,b)))

  打印輸出結果:

<class 'tuple'>
(1, 'a')
<class 'tuple'>
(2, 'b')
<class 'tuple'>
(3, 'c')
<class 'tuple'>
(4, 'd')
**************************************************
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM