Home > Backend Development > Python Tutorial > Introduction to the use of set() class

Introduction to the use of set() class

巴扎黑
Release: 2017-07-18 14:00:37
Original
2048 people have browsed it

set() set

Set creation method:

1 #集合是无序的不重复的2 li = [11,22,11,33]3 a = set(li)4 print(a)5 {33,22,11}
Copy after login
a = {11,22,33,44= {22,5577= {11,22,33,44= {22,55c =(a)

#discard移除集合不存在的元素不会报错,remove移除会报错
a.discard(99)
Copy after login
#intersection()取交集,intersection_update()同上
Copy after login

Implementation class of Set interface

To use Set Collections usually need to be declared as Set type and then instantiated through the implementation class of the Set interface. Commonly used implementation classes of the Set interface include the HashSet and TreeSet classes. The syntax format is as follows:

SetcollSet=new HashSet();

SetcollSet2=new TreeSet();

Since the objects in the Set collection are unordered, the results of traversing the Set collection are not the same as the order in which they were inserted into the Set collection.


The above is the detailed content of Introduction to the use of set() class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template