Detailed explanation of set() function in Python

小云云
Release: 2018-03-30 17:23:09
Original
3307 people have browsed it

The set() function creates an unordered set of non-repeating elements, which can perform relationship testing, delete duplicate data, and calculate intersection, difference, union, etc. Hope it helps everyone.

Syntax
set Syntax:

class set([iterable])
Copy after login

Parameter description:
iterable – iterable object object;

Return value
Return the new collection object.

Example
The following example shows how to use set:

x = set('runoob')
y = set('google')print(x)print(y)

a = x & y         # 交集print(a)

b = x | y         # 并集print(b)

c = x - y         # 差集print(c)
Copy after login

Output:

{'n', 'r', 'u', 'b', 'o'}{'l', 'e', 'g', 'o'}{'o'}{'g', 'r', 'e', 'b', 'l', 'n', 'u', 'o'}{'n', 'r', 'u', 'b'}
Copy after login

Related recommendations:

Introduction to the use of set() class

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!