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

Introduction to the use of set() class

Jul 18, 2017 pm 02:00 PM
gather

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!

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

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)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
Why is it difficult to implement collection-like functions in Go language? Why is it difficult to implement collection-like functions in Go language? Mar 24, 2024 am 11:57 AM

It is difficult to implement collection-like functions in the Go language, which is a problem that troubles many developers. Compared with other programming languages ​​such as Python or Java, the Go language does not have built-in collection types, such as set, map, etc., which brings some challenges to developers when implementing collection functions. First, let's take a look at why it is difficult to implement collection-like functionality directly in the Go language. In the Go language, the most commonly used data structures are slice and map. They can complete collection-like functions, but

How to optimize Java collection sorting performance How to optimize Java collection sorting performance Jun 30, 2023 am 10:43 AM

Java is a powerful programming language that is widely used in various types of software development. In Java development, scenarios that often involve sorting collections are involved. However, if performance optimization is not performed for collection sorting, the execution efficiency of the program may decrease. This article will explore how to optimize the performance of Java collection sorting. 1. Choose the appropriate collection class In Java, there are many collection classes that can be used for sorting, such as ArrayList, LinkedList, TreeSet, etc. Different collection classes are in

Common concurrent collections and thread safety issues in C# Common concurrent collections and thread safety issues in C# Oct 09, 2023 pm 10:49 PM

Common concurrent collections and thread safety issues in C# In C# programming, handling concurrent operations is a very common requirement. Thread safety issues arise when multiple threads access and modify the same data at the same time. In order to solve this problem, C# provides some concurrent collection and thread safety mechanisms. This article will introduce common concurrent collections in C# and how to deal with thread safety issues, and give specific code examples. Concurrent collection 1.1ConcurrentDictionaryConcurrentDictio

A Practical Guide to the Where Method in Laravel Collections A Practical Guide to the Where Method in Laravel Collections Mar 10, 2024 pm 04:36 PM

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

Add all elements from one collection to another using the addAll() method of the HashSet class Add all elements from one collection to another using the addAll() method of the HashSet class Jul 24, 2023 am 08:58 AM

Use the addAll() method of the HashSet class to add all elements in a collection to another collection. HashSet is an implementation class in the Java collection framework. It inherits from AbstractSet and implements the Set interface. HashSet is an unordered set based on a hash table, which does not allow duplicate elements. It provides many commonly used methods to operate elements in the collection, one of which is the addAll() method. The function of the addAll() method is to add the specified

Java Iterator vs. Iterable: A step into writing elegant code Java Iterator vs. Iterable: A step into writing elegant code Feb 19, 2024 pm 02:54 PM

Iterator interface The Iterator interface is an interface used to traverse collections. It provides several methods, including hasNext(), next() and remove(). The hasNext() method returns a Boolean value indicating whether there is a next element in the collection. The next() method returns the next element in the collection and removes it from the collection. The remove() method removes the current element from the collection. The following code example demonstrates how to use the Iterator interface to iterate over a collection: Listnames=Arrays.asList("John","Mary","Bob");Iterator

Clustering algorithm based on projection on convex sets (POCS) Clustering algorithm based on projection on convex sets (POCS) Apr 30, 2023 pm 12:22 PM

POCS: Projections to ConvexSets. In mathematics, a convex set is a set in which any line segment between any two points is within the set. Projection is the operation of mapping a point to a subspace in another space. Given a convex set and a point, you can operate by finding the projection of the point onto the convex set. The projection is the point in the convex set that is closest to the point and can be calculated by minimizing the distance between this point and any other point in the convex set. Since it is a projection, we can map the features to a convex set in another space, so that operations such as clustering or dimensionality reduction can be performed. This article reviews a clustering algorithm based on the convex set projection method, namely the POCS-based clustering algorithm. primitiveism

Efficiently calculate array intersection and union using PHP collection classes Efficiently calculate array intersection and union using PHP collection classes May 01, 2024 pm 09:06 PM

The intersection and union of arrays can be calculated efficiently using the PHP collection class. The specific steps are as follows: Use the intersect() method to calculate the intersection: elements that appear in two arrays at the same time. Use the union() method to calculate the union: the elements that appear in any array. Practical case: Compare shopping cart contents to understand users’ overlapping products and unique products.

See all articles