Python set() Function

Python 内置函数Python Built-in Functions


Description

set()The function creates an unordered collection of unique elements. It can perform relationship testing, remove duplicate data, and also calculate intersection, difference, union, etc.

Syntax

set syntax:

class set([iterable])

Parameter description:

  • iterable -- iterable object;

Return Value

Returns a new set object.

Examples

The following examples demonstrate how to use set:

>>>x = set('example') >>> y = set('google') >>> x, y (set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l'])) # duplicates are removed >>> x & y # intersection set(['o']) >>> x | y # union set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u']) >>> x - y # difference set(['r', 'b', 'u', 'n']) >>>

Python 内置函数Python Built-in Functions

Other Extensions