莊俊祥python字典

w3schols截圖

w3schols練習程式碼

a =	{ #台灣股票市場價值最大,莊俊祥python
  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])

w3schols字典用法



Method
方法
Description說明
clear()移除字典的Removes all the elements項目(items) 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

留言

  1. 希望你持續學習與探索,壯大自己,造福世界!

    回覆刪除

張貼留言

這個網誌中的熱門文章

莊俊祥python,print,input,字串[::-1]

莊俊祥Python視窗GUI自訂方法canvas.delete('all')

莊俊祥python集合set元組tuple清單或串列list迴圈輸出str,int