Detailed explanation of set syntax in python3
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.
The specific syntax is summarized as follows.
add (add elements)name = set(['Tom','Lucy','Ben']) name.add('Juny') print(name) #输出:{'Lucy', 'Juny', 'Ben', 'Tom'}
name = set(['Tom','Lucy','Ben']) name.clear() print(name) #输出:set()
name = set(['Tom','Lucy','Ben']) new_name = name.copy() print(new_name) #输出:{'Tom', 'Lucy', 'Ben'}
A = set([2,3,4,5]) B = set([3,4]) C = set([2]) n = n1.difference(n2,n3) print(n) #输出:{5} #返回A集合里面,在B和C集合中没有的元素,并生成新的集合
A = set([2,3,4,5]) B = set([4,5]) A.difference_update(B) print(A) #输出:{2, 3}
n = set([2,3,4]) n.discard(3) print(n) #输出:{2, 4}
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n = n1.intersection(n2) print(n) #输出:{4, 5}
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n1.intersection_update(n2) print(n1) #输出:{4, 5}
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) print(n1.isdisjoint(n2)) #输出:False
A = set([2,3]) B = set([2,3,4,5]) print(A.issubset(B)) #输出:True #A是B的子集
A = set([2,3]) B = set([2,3,4,5]) print(B.issuperset(A)) # 输出:True #B是A的父集
n = set([2,3,4,5]) n1 = n.pop() print(n,n1) # 输出:{3, 4, 5} 2
n = set([2,3,4,5]) n.remove(2) print(n) # 输出:{3, 4, 5}
A = set([2,3,4,5]) B = set([4,5,6,7]) print(A.symmetric_difference(B)) # 输出:{2, 3, 6, 7}
A = set([2,3,4,5]) B = set([4,5,6,7]) A.symmetric_difference_update(B) print(A) # 输出:{2, 3, 6, 7}
A = set([2,3,4,5]) B = set([4,5,6,7]) print(A.union(B)) # 输出:{2, 3, 4, 5, 6, 7}
A = set([2,3,4,5]) B = set([4,5,6,7]) A.update(B) print(A) # 输出:{2, 3, 4, 5, 6, 7}
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Methods to delete elements: 1. Use delete() to delete the specified element from the Set object, the syntax is "setObj.delete(value);"; 2. Use clear() to delete all elements in the Set object, the syntax is "setObj.delete(value);" "setObj.clear();".

This article is based on the basics of Python and introduces how to use dict and set. The dict using the key-value storage structure is very useful in Python. It is important to choose an immutable object as the key. The most commonly used key is a string.

1. Overview of the Map Collection Framework The Map collection framework is a key-value pair data structure that allows you to use keys to find and store values. Each key in the Map is unique and can only be associated with one value. Common implementations in the Map collection framework include HashMap, TreeMap and LinkedHashMap. 1.HashMapHashMap is the most widely used Map implementation in Java. It stores data based on hash tables. HashMap has excellent performance, and the time complexity of search and insertion operations is O(1), but it does not guarantee the order of elements. Demo code: Mapmap=newHashMap

Preface There are two very similar methods in the commonly used collection interface List in Java: Eset(intindex,Eelement); voidadd(intindex,Eelement); both of these methods insert specified elements at specified positions in the collection, then What is the difference between these two methods? Next, let’s take a look at the differences and similarities between these two methods through ArrayList, our commonly used collection implementation. First, let’s take a look at the similarities between these two methods in ArrayList. They will insert new elements at specified positions in the collection, such as the following Example: #Insert an F in the 2nd position of the collection #Insert Listlist= through the add method

The difference between Map and Set: key value and uniqueness: Map stores key-value pairs, and Set stores unique elements. Order: Among Maps and Sets, HashMap and HashSet are unordered sets, LinkedHashMap and LinkedHashSet are ordered sets, and TreeSet is sorted in order. Mutability: Map, LinkedHashMap and TreeSet are mutable collections, while HashSet and LinkedHashSet are immutable collections. Purpose: Map is used for key-value pair data, and Set is used for unique element data.

The set command syntax SETkeyid[FIELDnamevalue...][EXseconds][NX|XX](OBJECTgeojson)|(POINTlatlonz)|(BOUNDSminlatminlonmaxlatmaxlon)|(HASHgeohash)|(STRINGvalue) The set command is equivalent to the use of the hash command in redis. It is also a combination of key and id, but the difference is that Tile38's set command can also carry more other attributes, such as customizing the FIELD field, setting the EX validity period, etc., then we need to

Installation steps: 1. Make sure that Python3 has been installed and can be accessed through the command line; 2. Open the terminal and enter the "python3 -m ensurepip --upgrade" command to install pip; 3. Download the pip installation package from the official Python website; 4. Extract the downloaded pip installation package into a directory; 5. Open the terminal and navigate to the decompressed pip directory; 6. Run the "python3 setup.py install" command to install pip.
