White Science Python (15): basic data structure (set) (lower)

Life is short, I chose Python

The foregoing Portal

White learn Python (1): Opening

White Science Python (2): basic data type (on)

White Science Python (3): fundamental data types (lower)

White Science Python (4): Variable Basic Operation

White Science Python (5): base operator (on)

White Science Python (6): base operator (lower)

White Science Python (7): based flow control (on)

White Science Python (8): the basis of flow control (lower)

White Science Python (9): basic data structure (list) (a)

White Science Python (10): basic data structure (list) (lower)

White Science Python (11): basic data structure (tuples)

White Science Python (12): basic data structure (dictionary) (a)

White Science Python (13): basic data structure (dictionary) (lower)

White Science Python (14): basic data structure (set) (a)

A collection of built-in method

And a collection of lists, dictionaries, tuples, etc., as are providing a lot of built-in method.

Since it is a collection, then we look at this classic Wayne diagram (Venn diagram):

Specific codes are as follows:

set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 7, 9}

# 求交集
set3 = set1.intersection(set2)
print('交集:', set3)

# 求并集
set4 = set1.union(set2)
print('并集:', set4)

# 做差
set5 = set1.difference(set2)
print('做差:', set5)

The results are as follows:

交集: {4, 5, 6}
并集: {1, 2, 3, 4, 5, 6, 7, 9}
做差: {1, 2, 3}

set.add()

Role: add elements to the collection

set6 = {1, 2, 3}
set6.add(4)
print(set6)
set6.add('python')
print(set6)
set6.add((1, 2))
print(set6)

The results are as follows:

{1, 2, 3, 4}
{1, 2, 3, 4, 'python'}
{(1, 2), 1, 2, 3, 4, 'python'}

set.update()

Role: add elements to the collection

set7 = {1, 2}
set7.update({3, 4, 'python', (4, 5)})
print(set7)

The results are as follows:

{1, 2, 3, 4, 'python', (4, 5)}

It seems to appear update()and add()function is the same, except that add()the parameter can be a single element, and update()the parameter set is a collection.

set.pop()

Role: to remove random elements.

set7.pop()
print(set7)

The results are as follows:

{2, 3, 4, 'python', (4, 5)}

Random deleted, personal sensory function may be a bit no practical effect, after all, is a random deleted. . .

set.remove()

Role: Removes the specified element.

Compared above pop()random deletion, deleting elements when more of us are using remove()or below us it will be introduced discard().

set8 = {1, 2, 3, 4}
set8.remove(4)
print(set8)

The results are as follows:

{1, 2, 3}

If you remove the element does not exist, it will direct error.

set8.remove(9)

The results are as follows:

Traceback (most recent call last):
  File "F:/project/python-learning/base-data-set/Demo1.py", line 34, in <module>
    set8.remove(9)
KeyError: 9

set.discard()

Role: Delete collection at the specified element, the element does not exist, do nothing.

set8.discard(9)
print(set8)

The results are as follows:

{1, 2, 3}

set.clear()

Role: Clear all elements in the collection, and does not clear the collection.

set9 = {1, 2, 3}
set9.clear()
print(set9)

The results are as follows:

set()

set.isdisjoint()

Role: to determine whether the two sets contain the same elements, if there is no return True, otherwise False.

set10 = {1, 2, 3}
set11 = {1, 2}
set12 = {4, 5}
print(set10.isdisjoint(set11))
print(set10.isdisjoint(set12))

The results are as follows:

False
True

set.issubset()

Action: determining whether the subset specified collection set of parameters for the method.

print(set11.issubset(set10))
print(set12.issubset(set10))

The results are as follows:

True
False

set.issuperset()

Action: determining a set of parameters of the method is the specified subset of the set.

print(set10.issuperset(set11))
print(set10.issuperset(set12))

The results are as follows:

True
False

Note Oh, issubset()and issuperset()is not the same oh, looks are a subset of the judgment, but the meaning of the parameters is not the same.

Fellow students have to knock yourself codes ohhh

Sample Code

This series of small series all the code will be placed on code management repository Github and Gitee, to facilitate access.

Sample Code -Github

Sample Code -Gitee

Guess you like

Origin www.cnblogs.com/babycomeon/p/11803062.html