- Learning Python for Forensics
- Preston Miller Chapin Bryce
- 401字
- 2021-08-20 10:17:02
Dictionaries
Dictionaries, otherwise known as dict, are another common Python data container. Unlike lists, this object does not add data in a linear fashion. Instead, data is stored as key and value pairs, where you can create and name unique keys to act as an index for stored values. It is important to note that, in Python 2, dictionaries do not preserve the order in which items are added to it. This is no longer true as of Python 3.6.5, though in general, we should not rely on the dict() object maintaining order for us. These objects are used heavily in forensic scripting, as they allow us to store data by name in a single object; otherwise, we may be left assigning a lot of new variables. By storing data in dictionaries, it is possible to have one variable contain very structured data.
We can define a dictionary by using curly braces ({}), where each key and value pair is delimited by a colon. Additionally, we can use the dict() class constructor to instantiate dictionary objects. Calling a value from a dictionary is accomplished by specifying the key in brackets following the dictionary object. If we supply a key that does not exist, we will receive a KeyError (notice that we have assigned our dictionary to a variable, a). While we have not introduced variables at this point, it is necessary to highlight some of the functions that are specific to dictionaries:
>>> type({'Key Lime Pie': 1, 'Blueberry Pie': 2})
<class 'dict'>
>>> dict((['key_1', 'value_1'],['key_2', 'value_2']))
{'key_1': 'value_1', 'key_2': 'value_2'}
>>> a = {'key1': 123, 'key2': 456}
>>> a['key1']
123
We can add or modify the value of a preexisting key in a dictionary by specifying a key and setting it equal to another object. We can remove objects using the pop() function, similar to the list pop() function, to remove an item in a dictionary by specifying its key instead of an index:
>>> a['key3'] = 789
>>> a
{'key1': 123, 'key2': 456, 'key3': 789}
>>> a.pop('key1')
123
>>> a
{'key2': 456, 'key3': 789}
The keys() and values() functions return a list of keys and values in the dictionary. We can use the items() function to return a list of tuples containing each key and value pair. These three functions are often used for conditionals and loops:
>>> a.keys()
dict_keys(['key2', 'key3'])
>>> a.values()
dict_values([456, 789])
>>> a.items()
dict_items([('key3', 789), ('key2', 456)])
- 工業(yè)互聯(lián)網(wǎng)安全防護(hù)與展望
- 零信任網(wǎng)絡(luò):在不可信網(wǎng)絡(luò)中構(gòu)建安全系統(tǒng)
- 走進(jìn)新安全:讀懂網(wǎng)絡(luò)安全威脅、技術(shù)與新思想
- 同態(tài)密碼學(xué)原理及算法
- 網(wǎng)絡(luò)安全三十六計(jì):人人該懂的防黑客技巧
- Kali Linux Network Scanning Cookbook(Second Edition)
- 從實(shí)踐中學(xué)習(xí)Kali Linux滲透測(cè)試
- 云原生安全與DevOps保障
- 軟件安全保障體系架構(gòu)
- 網(wǎng)絡(luò)安全設(shè)計(jì)、配置與管理大全
- 數(shù)字政府網(wǎng)絡(luò)安全合規(guī)性建設(shè)指南:密碼應(yīng)用與數(shù)據(jù)安全
- 云計(jì)算安全:關(guān)鍵技術(shù)、原理及應(yīng)用
- 網(wǎng)絡(luò)空間安全實(shí)戰(zhàn)基礎(chǔ)
- Manga Studio 5 Beginner's Guide
- 網(wǎng)絡(luò)入侵檢測(cè)系統(tǒng)原理與應(yīng)用