Dictionary
今天想分享如何用 dictionary 字典方式存資料。
key and Value 如何使用宣槓取直
- collection of of {key:value} pairs
- no duplicate
- allow changeable and ordered
declare dictionary 宣告 dict
It's an unorder , it's not next to each other, so we can't use index like we use in list. Dictionary will have a a key and value look like this:
syntax:
declare:
dictname= {key:value}
get key:print(dictname['key'])
Access and Get value 取我們的直:
1 | dict={ |
取 key 的直: print(dict['keyA'])
=> 1
取所有直 print(dict)
=> {'keyA': 1, 'keyB': 2}