Collection type of Python combined data type

Unit overview
Mainly solve the problem: let the program handle a set of data better
Three types of important combination data types: collection type, sequence type and dictionary type

After studying this chapter, we can build patterns of sets, sequences and dictionaries in our minds to express and process a set of data

1. Definition

A collection is an unordered combination of multiple elements
. The elements in the
collection cannot be repeated . The elements in the collection are immutable data types-otherwise it may break the previous one

Immutable data types are: integer, floating point, negative, string, tuple

2. Representation

Sets are represented by curly braces {}, and the elements are separated by commas. To
create a set, use {} or set()-to create an empty set, you must use set()
.

A = {
    
    "python",123,("python",123)}
B = set("pypy123")
print(A)
print(B)

Output
{'python', ('python', 123), 123}
{'3', '1', '2','p','y'}//Note that the elements in the set are not ordered

3. Set operators

① Combine |
② Poor-
③ Cross &
④ Combine and subtract ^
Insert picture description here

⑤ The relational operator judges whether one set is contained in another, and returns True/False
<<=> >=
⑥ Enhanced operator-update S with the operation result of S and T
Insert picture description here

Example

A = {
    
    "p","y","python",123,("python",123)}
B = set("pypy123")
print(A-B)

Result
{'python', 123, ('python', 123)}
4. Set processing method
Add element S.add(x)
delete element S.discard(x) and S.remove(x) delete element that does not exist Will report an error, the latter will
empty the collection S.clear()
randomly take out elements and update the collection S.pop() If S is empty, return an exception
Copy the collection S.copy() return a copy of the collection S
Find the number of elements len( S)
Determine whether the element is in the set S x in S returns True/False corresponding to x not in S
converted to the set type set(x)

When using for in to traverse a collection, the order of the
collection is uncertain (the collection has a storage order when it is defined, but this order cannot be used by programmers)
.

A = {
    
    "A","B",123}
for item in A:
    print(item,end="")

Output
B123A

May be used to traverse the collection while
Example

A = {
    
    "A","B",123}
try:
    while True:
        print(A.pop(),end="")
except:
    pass
print("",A)

Output
B123A set()
5. Set type application scenario
① Comparison of containment relationships, that is, how to judge whether another set of data or other data is in it if one set of data is known
.

sss = "p" in {
    
    "p","y",123}
print(sss)
ttt = {
    
    "p"} > {
    
    "p","y",123}
print(ttt)

Output
True
False
② data deduplication, i.e. a list of the unique type, into a type of non-repeating
Example

ls = ["p","p","y","y",123]#列表
s = set(ls)#集合
lt = list(s)#列表
print(lt)

Output
['p', 123,'y']

Source: BIT Python MOOC

Guess you like

Origin blog.csdn.net/weixin_44997802/article/details/108098277