- Practical Data Wrangling
- Allan Visochek
- 258字
- 2021-07-02 15:16:10
Dictionaries
A dictionary, like an array, is a collection of items; however, the items in a dictionary are referenced by strings called keys, rather than their order in a list. Another way to think of a dictionary is as a collection of key-value pairs, where the keys are used to retrieve the values.
To create a dictionary, use the following syntax where <key1>, <key2> are strings and value1, value2 are any values:
{<key1>:<value1>,<key2>:<value2>,<key3>:<value3>}
The following line will create a dictionary called d with two key-value pairs:
>> d = {"some_number": 1, "sup?": "yo!"}
>> print(d)
You can access a specific value of a dictionary using the following syntax:
<dictionary name>[<key>]
For example:
>> print(d["sup?"])
>> print(d["some_number"])
You can also set the value of a key by assigning a value to that key, as the following demonstrates:
>> d["some_number"] = 5
>> print(d["some_number"])
If you assign a value to a key that does not yet exist in a particular dictionary, the key will be added to the dictionary:
>> d["new_key"] = "I'm a new value"
>> print(d["new_key"])
On an intuitive level, the big advantage of Python dictionaries comes from the ability to store and reference items by key. It is easier to remember a name than a number. If, for example, you are working with a spreadsheet that has 100 columns, it is much more convenient to retrieve an entry by its column header than its position.
- 高性能混合信號(hào)ARM:ADuC7xxx原理與應(yīng)用開發(fā)
- WOW!Illustrator CS6完全自學(xué)寶典
- 精通Windows Vista必讀
- STM32嵌入式微控制器快速上手
- Learning C for Arduino
- JavaScript典型應(yīng)用與最佳實(shí)踐
- 電腦日常使用與維護(hù)322問(wèn)
- Visual C++項(xiàng)目開發(fā)案例精粹
- Cloud Security Automation
- Mastering Text Mining with R
- 智慧未來(lái)
- FANUC工業(yè)機(jī)器人配置與編程技術(shù)
- 步步驚“芯”
- 玩機(jī)器人 學(xué)單片機(jī)
- Windows 7來(lái)了