Home  >  Article  >  Backend Development  >  Detailed explanation of set() function in Python

Detailed explanation of set() function in Python

小云云
小云云Original
2018-03-30 17:23:093362browse

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])

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)

Output:

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

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!

Statement:
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