- 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.
- Node Security
- 攻守道:企業(yè)數(shù)字業(yè)務(wù)安全風險與防范
- 計算機網(wǎng)絡(luò)安全技術(shù)(第6版·慕課版)
- Metasploit Penetration Testing Cookbook(Second Edition)
- 大型互聯(lián)網(wǎng)企業(yè)安全架構(gòu)
- Web安全與攻防入門很輕松(實戰(zhàn)超值版)
- 同態(tài)密碼學原理及算法
- 從0到1:CTFer成長之路
- Kerberos域網(wǎng)絡(luò)安全從入門到精通
- 無線傳感器網(wǎng)絡(luò)安全與加權(quán)復(fù)雜網(wǎng)絡(luò)抗毀性建模分析
- 網(wǎng)絡(luò)空間安全導(dǎo)論
- 信息系統(tǒng)安全等級化保護原理與實踐
- 網(wǎng)絡(luò)入侵檢測系統(tǒng)原理與應(yīng)用
- 計算機網(wǎng)絡(luò)安全與應(yīng)用技術(shù)(第2版)
- 反黑風暴:黑客社會工程學攻防演練