- Mastering Python Networking
- Eric Chou
- 242字
- 2021-07-02 21:42:32
Sets
A set is used to contain an unordered collection of objects. Unlike lists and tuples, sets are unordered and cannot be indexed by numbers. But there is one character that makes sets standout and useful: the elements of a set are never duplicated. Imagine if you have a list of IPs that you need to put in an access list of. The only problem in this list of IPs is that they are full of duplicates. Now, think about how you would use a loop to sort it out for the unique items, the set built-in type would allow you to eliminate the duplicate entries with just one line of code. To be honest, I do not use set that much, but when I need it, I am always very thankful this exists. Once the set or sets are created, they can be compared with each other ones using the union, intersection, and differences:
>>> a = "hello"
>>> set(a)
{'h', 'l', 'o', 'e'}
>>> b = set([1, 1, 2, 2, 3, 3, 4, 4])
>>> b
{1, 2, 3, 4}
>>> b.add(5)
>>> b
{1, 2, 3, 4, 5}
>>> b.update(['a', 'a', 'b', 'b'])
>>> b
{1, 2, 3, 4, 5, 'b', 'a'}
>>> a = set([1, 2, 3, 4, 5])
>>> b = set([4, 5, 6, 7, 8])
>>> a.intersection(b)
{4, 5}
>>> a.union(b)
{1, 2, 3, 4, 5, 6, 7, 8}
>>> 1 *
{1, 2, 3}
>>>
- Vue.js 2 and Bootstrap 4 Web Development
- 數(shù)據(jù)庫系統(tǒng)原理及MySQL應(yīng)用教程
- Java從入門到精通(第5版)
- Koa開發(fā):入門、進(jìn)階與實(shí)戰(zhàn)
- Kotlin Standard Library Cookbook
- Learning FuelPHP for Effective PHP Development
- 劍指Java:核心原理與應(yīng)用實(shí)踐
- 零基礎(chǔ)入門學(xué)習(xí)Python(第2版)
- Vue.js 2 Web Development Projects
- 現(xiàn)代C:概念剖析和編程實(shí)踐
- AI自動化測試:技術(shù)原理、平臺搭建與工程實(shí)踐
- Three.js權(quán)威指南:在網(wǎng)頁上創(chuàng)建3D圖形和動畫的方法與實(shí)踐(原書第4版)
- Python Social Media Analytics
- Socket.IO Cookbook
- Oracle Database 12c DBA官方手冊(第8版)