Home Backend Development Python Tutorial Detailed explanation of set syntax in python3

Detailed explanation of set syntax in python3

May 03, 2017 pm 03:45 PM
python3 set

This article mainly summarizes the relevant information about the syntax of set (set) in python3. The article gives detailed sample code, which has certain reference value for everyone. Friends who need it can take a look below.

Introduction

set Gu Mingsiyi is a set, and the elements of the set are unique and unordered. Putting some elements in a { ​​} forms a set. The set can be of multiple data types (but not lists, sets, dictionaries, but can be tuples)

A set is an unordered non-repeating element of sets. Basic functionality includes relationship testing and elimination of duplicate elements. Set objects also support mathematical operations such as union, intersection, difference and sysmmetric difference.

Braces or the set() function can be used to create a set. Note: To create an empty collection, you must use set() instead of {}. {} is used to create an empty dictionary;

The specific syntax is summarized as follows.

add (add elements)

name = set(['Tom','Lucy','Ben'])
name.add('Juny')
print(name)

#输出:{'Lucy', 'Juny', 'Ben', 'Tom'}
Copy after login

clear (clear all elements)

name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

#输出:set()
Copy after login

copy (copy set collection)

name = set(['Tom','Lucy','Ben'])
new_name = name.copy()
print(new_name)

#输出:{'Tom', 'Lucy', 'Ben'}
Copy after login

difference (return Different elements in two or more sets and generate a new set)

A = set([2,3,4,5])
B = set([3,4])
C = set([2])
n = n1.difference(n2,n3)
print(n)

#输出:{5}
#返回A集合里面,在B和C集合中没有的元素,并生成新的集合
Copy after login

difference_update (Delete the elements in set A that exist in set B.)

A = set([2,3,4,5])
B = set([4,5])
A.difference_update(B)
print(A)

#输出:{2, 3}
Copy after login

discard (move Remove elements)

n = set([2,3,4])
n.discard(3)
print(n)

#输出:{2, 4}
Copy after login

intersection(Get the intersection and generate a new set)

n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
n = n1.intersection(n2)
print(n)

#输出:{4, 5}
Copy after login

intersection_update(Get the intersection and modify the original set)

n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
n1.intersection_update(n2)
print(n1)

#输出:{4, 5}
Copy after login

isdisjoint(Judge the intersection, Returns False if yes, True if not)

n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
print(n1.isdisjoint(n2))

#输出:False
Copy after login

issubset(Judge subset)

A = set([2,3])
B = set([2,3,4,5])
print(A.issubset(B))

#输出:True
#A是B的子集
Copy after login

issuperset(Judge parent set)

A = set([2,3])
B = set([2,3,4,5])
print(B.issuperset(A))

# 输出:True
#B是A的父集
Copy after login

pop(Randomly remove an element)

n = set([2,3,4,5])
n1 = n.pop()
print(n,n1)

# 输出:{3, 4, 5} 2
Copy after login

remove(remove the specified element)

n = set([2,3,4,5])
n.remove(2)
print(n)

# 输出:{3, 4, 5}
Copy after login

symmetric_difference(take the intersection and generate a new set)

A = set([2,3,4,5])
B = set([4,5,6,7])
print(A.symmetric_difference(B))

# 输出:{2, 3, 6, 7}
Copy after login

symmetric_difference_update(take the intersection and change the original set)

A = set([2,3,4,5])
B = set([4,5,6,7])
A.symmetric_difference_update(B)
print(A)

# 输出:{2, 3, 6, 7}
Copy after login

union (take the union and generate a new set)

A = set([2,3,4,5])
B = set([4,5,6,7])
print(A.union(B))

# 输出:{2, 3, 4, 5, 6, 7}
Copy after login

update (take the union and change the original set)

A = set([2,3,4,5])
B = set([4,5,6,7])
A.update(B)
print(A)

# 输出:{2, 3, 4, 5, 6, 7}
Copy after login

Summary

The above is the detailed content of Detailed explanation of set syntax in python3. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

How to delete elements from set in javascript How to delete elements from set in javascript Jan 12, 2022 am 10:56 AM

How to delete elements from set in javascript

Inventory of common uses of dict and set in Python programming Inventory of common uses of dict and set in Python programming Jul 25, 2023 pm 04:52 PM

Inventory of common uses of dict and set in Python programming

What is the difference between the set method and the add method in List in java? What is the difference between the set method and the add method in List in java? Apr 19, 2023 pm 07:49 PM

What is the difference between the set method and the add method in List in java?

Comparison of Java Map and other collection frameworks: advantages and disadvantages analysis and application scenario guide Comparison of Java Map and other collection frameworks: advantages and disadvantages analysis and application scenario guide Feb 19, 2024 pm 10:24 PM

Comparison of Java Map and other collection frameworks: advantages and disadvantages analysis and application scenario guide

How to implement the Set command of Springboot integrated Tile client How to implement the Set command of Springboot integrated Tile client May 19, 2023 pm 01:37 PM

How to implement the Set command of Springboot integrated Tile client

Java Collection Framework Practical Guide: Teach you step by step how to use the framework and solve practical problems Java Collection Framework Practical Guide: Teach you step by step how to use the framework and solve practical problems Feb 23, 2024 am 10:52 AM

Java Collection Framework Practical Guide: Teach you step by step how to use the framework and solve practical problems

Common uses of set Common uses of set Oct 24, 2023 am 11:25 AM

Common uses of set

See all articles