Alibaba Cloud Python Training Camp (day6)


Introduction

Set in Python is similar to dict. It is also a set of keys, but does not store value. Since the key cannot be repeated, there is no duplicate key in the set. Note that the key is an immutable type, that is, a hash value.
Sequence types include strings, lists, tuples, sets, and dictionaries. These sequences support some common operations, but what is more special is that sets and dictionaries do not support indexing, slicing, addition, and multiplication operations.



One, collection

1. Collection creation

先创建对象再加入元素。
在创建空集合的时候只能使用s = set(),因为s = {}创建的是空字典。

The demo code is as follows:

basket = set()
basket.add('zmt')
basket.add('zgk')
print(basket)  
# {'zmt', 'zgk'}

直接把一堆元素用花括号括起来{元素1, 元素2, ..., 元素n}。
重复元素在set中会被自动被过滤。

The demo code is as follows:

basket = {
    
    'zmt', 'zgk', 'zmt', 'wj'}
print(basket)  
# {'zmt', 'zgk', 'wj'}

使用set(value)工厂函数,把列表或元组转换成集合。

The demo code is as follows:

a = set('abracadabra')
print(a)  
# {'r', 'b', 'd', 'c', 'a'}

b = set(("wj", "wj", "zmt", "zgk"))
print(b)  
# {'wj', 'zmt', 'zgk'}

Example: remove duplicate elements in the list

The demo code is as follows:

lst = [0, 1, 2, 3, 4, 5, 5, 3, 1]
temp = []
for item in lst:
    if item not in temp:
        temp.append(item)
        
print(temp)  
# [0, 1, 2, 3, 4, 5]

a = set(lst)
print(list(a))  
# [0, 1, 2, 3, 4, 5]

Two characteristics of the collection are found from the results:

无序 (unordered) 
唯一 (unique)。

Since set stores an unordered collection, we cannot create indexes or perform slice operations for the collection, and there are no keys to get the value of the elements in the collection, but we can judge whether an element is in the collection.

2. Access the values ​​in the collection

  1. You can use the len() built-in function to get the size of the collection.
  2. You can use for to read the data in the collection one by one.
  3. You can judge whether an element already exists in the collection by in or not in

The demo code is as follows:

s = set(['Google', 'Baidu', 'Taobao'])
print(len(s)) 
# 3

s = set(['Google', 'Baidu', 'Taobao'])
for item in s:
    print(item)
# Baidu
# Google
# Taobao

s = set(['Google', 'Baidu', 'Taobao'])
print('Taobao' in s)  # True
print('Facebook' not in s)  # True

3. Built-in methods of collection

(1).set.add(elmnt) is used to add elements to the collection. If the added element already exists in the collection, no operation is performed.

The demo code is as follows:

fruits = {
    
    "apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)  
# {'orange', 'cherry', 'banana', 'apple'}

(2).set.update(set) is used to modify the current collection. You can add new elements or collections to the current collection. If the added element already exists in the collection, the element will only appear once, and the duplicate will be ignored .

The demo code is as follows:

x = {
    
    "apple", "banana", "cherry"}
y = {
    
    "google", "baidu", "apple"}
x.update(y)
print(x)
# {'cherry', 'banana', 'apple', 'google', 'baidu'}

(3).

set.remove(item) 用于移除集合中的指定元素。如果元素不存在,则会发生错误。
set.discard(value) 用于移除指定的集合元素。remove() 方法在移除一个不存在的元素时会发生错误,而 discard() 方法不会。
set.pop() 用于随机移除一个元素。

The demo code is as follows:

fruits = {
    
    "apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)  # {'apple', 'cherry'}


fruits = {
    
    "apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)  # {'apple', 'cherry'}


fruits = {
    
    "apple", "banana", "cherry"}
x = fruits.pop()
print(fruits)  # {'cherry', 'apple'}
print(x)  # banana

(4).

Since a set is a set of unordered and no repeating elements, two or more sets can do set operations in a mathematical sense.

set.intersection(set1, set2) 返回两个集合的交集。
set1 & set2 返回两个集合的交集。
set.intersection_update(set1, set2) 交集,在原始的集合上移除不重叠的元素。

The demo code is as follows:

a = set('abracadabra')
b = set('alacazam')
print(a)  # {'r', 'a', 'c', 'b', 'd'}
print(b)  # {'c', 'a', 'l', 'm', 'z'}

c = a.intersection(b)
print(c)  # {'a', 'c'}
print(a & b)  # {'c', 'a'}
print(a)  # {'a', 'r', 'c', 'b', 'd'}

a.intersection_update(b)
print(a)  # {'a', 'c'}

(5).

set.union(set1, set2) 返回两个集合的并集。
set1 | set2 返回两个集合的并集。

The demo code is as follows:

a = set('abracadabra')
b = set('alacazam')
c = a.union(b)
print(c)  
# {'c', 'a', 'd', 'm', 'r', 'b', 'z', 'l'}


print(a | b)  
# {'l', 'd', 'm', 'b', 'a', 'r', 'z', 'c'}

(6).

set.difference(set) 返回集合的差集。
set1 - set2 返回集合的差集。
set.difference_update(set) 集合的差集,直接在原来的集合中移除元素,没有返回值。

The demo code is as follows:

a = set('abracadabra')
b = set('alacazam')

c = a.difference(b)
print(c)  # {'b', 'd', 'r'}

print(a - b)  # {'d', 'b', 'r'}

print(a)  # {'r', 'd', 'c', 'a', 'b'}
a.difference_update(b)
print(a)  # {'d', 'r', 'b'}

(7).

 set.symmetric_difference(set)返回集合的异或。
 set1 ^ set2 返回集合的异或。
 set.symmetric_difference_update(set)移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中。

The demo code is as follows:

a = set('abracadabra')
b = set('alacazam')

c = a.symmetric_difference(b)
print(c)  # {'m', 'r', 'l', 'b', 'z', 'd'}
print(a ^ b)  # {'m', 'r', 'l', 'b', 'z', 'd'}

print(a)  # {'r', 'd', 'c', 'a', 'b'}
a.symmetric_difference_update(b)
print(a)  # {'r', 'b', 'm', 'l', 'z', 'd'}

(8).

set.issubset(set)判断集合是不是被其他集合包含,如果是则返回 True,否则返回 False。
set1 <= set2 判断集合是不是被其他集合包含,如果是则返回 True,否则返回 False。

The demo code is as follows:

x = {
    
    "a", "b", "c"}
y = {
    
    "f", "e", "d", "c", "b", "a"}
z = x.issubset(y)
print(z)  # True

x = {
    
    "a", "b", "c"}
y = {
    
    "f", "e", "d", "c", "b"}
z = x.issubset(y)
print(z)  # False
print(x <= y)  # False

(9).

set.issuperset(set)用于判断集合是不是包含其他集合,如果是则返回 True,否则返回 False。
set1 >= set2 判断集合是不是包含其他集合,如果是则返回 True,否则返回 False.

The demo code is as follows:

x = {
    
    "f", "e", "d", "c", "b", "a"}
y = {
    
    "a", "b", "c"}
z = x.issuperset(y)
print(z)  # True
print(x >= y)  # True

x = {
    
    "f", "e", "d", "c", "b"}
y = {
    
    "a", "b", "c"}
z = x.issuperset(y)
print(z)  # False
print(x >= y)  # False

(10).set.isdisjoint(set) is used to judge whether two sets are disjoint. If it is, it returns True, otherwise it returns False.

The demo code is as follows:

x = {
    
    "f", "e", "d", "c", "b"}
y = {
    
    "a", "b", "c"}
z = x.isdisjoint(y)
print(z)  # False

4. Immutable collections

Python provides an implementation version of a set that cannot change elements, that is, elements cannot be added or deleted. The type is named frozenset. It should be noted that frozenset can still perform collection operations, but the method with update cannot be used.

frozenset([iterable]) 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。

The demo code is as follows:

a = frozenset(range(10))  # 生成一个新的不可变集合
print(a)  
# frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})

b = frozenset('lsgogroup')
print(b)  
# frozenset({'g', 's', 'p', 'r', 'u', 'o', 'l'})

Second, the sequence

Built-in functions for sequences

(1). Conversion function

list(sub) 把一个可迭代对象转换为列表。
tuple(sub) 把一个可迭代对象转换为元组。
str(obj) 把obj对象转换为字符串.

There is no code for these three conversion functions.


(2).

len(s) 返回对象(字符、列表、元组等)长度或元素个数。
max(sub)返回序列或者参数集合中的最大值
min(sub)返回序列或参数集合中的最小值
sum(iterable[, start=0]) 返回序列iterable与可选参数start的总和。

The demo code is as follows:

c = 'I Love LsgoGroup'
print(len(c))  # 16

print(max([-8, 99, 3, 7, 83]))  
# 99

print(min('IloveLsgoGroup'))  
# G

print(sum([1, 3, 5, 7, 9], 10))  
# 35

(3).

sorted(iterable, key=None, reverse=False) 对所有可迭代的对象进行排序操作。iterable -- 可迭代对象。
  key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
  返回重新排序的列表。

The demo code is as follows:

x = [-8, 99, 3, 7, 83]
print(sorted(x))  # [-8, 3, 7, 83, 99]
print(sorted(x, reverse=True))  # [99, 83, 7, 3, -8]

t = ({
    
    "age": 20, "name": "a"}, {
    
    "age": 25, "name": "b"}, {
    
    "age": 10, "name": "c"})
x = sorted(t, key=lambda a: a["age"])
print(x)
# [{'age': 10, 'name': 'c'}, {'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}]

(4)

reversed(seq) 函数返回一个反转的迭代器。
  seq -- 要转换的序列,可以是 tuple, string, list 或 range。

The demo code is as follows:

r = range(5, 9)
print(list(reversed(r)))
# [8, 7, 6, 5]

(5).enumerate(sequence, [start=0]) is used to combine a traversable data object (such as a list, tuple or string) into an index sequence, and list data and data subscripts at the same time, generally used In the for loop.

The demo code is as follows:

seasons = ['Spring', 'Summer', 'Fall', 'Winter']
a = list(enumerate(seasons))
print(a)  
# [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

(6).

zip(iter1 [,iter2 [...]])
  用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存。
  我们可以使用 list() 转换来输出列表。
  如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

The demo code is as follows:

a = [1, 2, 3]
b = [4, 5, 6]
c = [4, 5, 6, 7, 8]

zipped = zip(a, b)
print(zipped)  # <zip object at 0x000000C5D89EDD88>
print(list(zipped))  # [(1, 4), (2, 5), (3, 6)]
zipped = zip(a, c)
print(list(zipped))  # [(1, 4), (2, 5), (3, 6)]

a1, a2 = zip(*zip(a, b))
print(list(a1))  # [1, 2, 3]
print(list(a2))  # [4, 5, 6]


Three, summary

In TASK2, I learned a series of python containers for storing data. The most important thing is that I learned many built-in methods, which gave me a lot of knowledge. The biggest problem now is how to remember all the built-in methods. I still have some Remember, in future studies, I am going to study the back while reviewing the front. Come on!

Guess you like

Origin blog.csdn.net/qq_44250569/article/details/108876024