- Learning Python for Forensics
- Preston Miller Chapin Bryce
- 246字
- 2021-08-20 10:17:02
Sets and tuples
Sets are similar to lists in that they contain a list of elements, though they must be unique items. With this, the elements must be immutable, meaning that the value must remain constant. For this, sets are best used on integers, strings, Boolean, floats, and tuples as elements. Sets do not index the elements, and therefore we cannot access the elements by their location in the set. Instead, we can access and remove elements through the use of the pop() method mentioned for the list method. Tuples are also similar to lists, though they are immutable. Built using parenthesis in lieu of brackets, elements do not have to be unique and of any data type:
>>> type(set([1, 4, 'asd', True]))
<class 'set'>
>>> g = set(["element1", "element2"])
>>> g
{'element1', 'element2'}
>>> g.pop()
'element2'
>>> g
{'element1'}
>>> tuple('foo')
('f', 'o' , 'o')
>>> ('b', 'a', 'r')
('b', 'a', 'r')
>>> ('Chapter1', 22)[0]
Chapter1
>>> ('Foo', 'Bar')[-1]
Bar
The important difference between a tuple and a list is that a tuple is immutable. This means that we cannot change a tuple object. Instead, we must replace the object completely or cast it to a list, which is mutable. This casting process is described in the next section. Replacing an object is very slow since the operation to add a value to a tuple is tuple = tuple + ('New value',), noting that the trailing comma is required to denote that this addition is a tuple.
- Web漏洞分析與防范實戰(zhàn):卷1
- 數(shù)字身份與元宇宙信任治理
- SASE原理、架構與實踐
- Kali Linux CTF Blueprints
- 信息系統(tǒng)安全檢測與風險評估
- 可信計算3.0工程初步
- INSTANT Burp Suite Starter
- 開發(fā)者的Web安全戒律:真實威脅與防御實踐
- Getting Started with FortiGate
- .NET安全攻防指南(上冊)
- 學電腦安全與病毒防范
- 電腦安全與攻防入門很輕松(實戰(zhàn)超值版)
- 無線傳感器網(wǎng)絡安全與加權復雜網(wǎng)絡抗毀性建模分析
- 持續(xù)集成:軟件質量改進和風險降低之道
- Mastering Metasploit