林佳誼Python字典Dictionaries
w3scholls截圖
w3scholls練習程式碼
#字典 keys:values, 劉任昌
#w3schools原來 字串:字串,改成 整數:字串
a = { #市場價值最大的五家公司
2330: "台積電",
2317: "鴻海",
2454: "聯發科",
2412: "中華電",
6505: "台塑化"
}
print(a)
print(a[6505])
print(a.get(2330))#功能同 a[2330]
print(a.keys()) #keys()方法列出key搜尋鍵
print(a.values()) #keys()方法列出values值
b = a.copy()
print("列出b " + str(b))
print(b[2317])
print(b.clear())
a.update({2308: "台達電"})
print(a.values())
print("用迴圈列出字典a的所有值")
for t in a:
print(a[t])字典方法Dictionary Methods
| Method | Description |
|---|---|
| clear() | Removes all the elements from the dictionary |
| copy() | Returns a copy of the dictionary |
| fromkeys() | Returns a dictionary with the specified keys and value |
| get() | Returns the value of the specified key |
| items() | Returns a list containing a tuple for each key value pair |
| keys() | Returns a list containing the dictionary's keys |
| pop() | Removes the element with the specified key |
| popitem() | Removes the last inserted key-value pair |
| setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
| update() | Updates the dictionary with the specified key-value pairs |
| values() | Returns a list of all the values in the dictionary |
320 dict. https://jia-yi-lin.blogspot.com/2023/03/pythondictionaries.html
回覆刪除