Detailed Explanation of the Set of Python

  • 2021-12-04 10:42:35
  • OfStack

Basic operations of catalog collections 1, add elements add () update () 2, remove elements remove () clear () 3, intersections of collections What is an intersection? intersection () 3, union of sets What is union? union () Summary

A set (set) is an unordered sequence of non-repeating elements.

You can create collections using curly braces {} or the set () function.


student = {' Xiao Ming ', 'xiaohong', 'adm'}
print('student Data type of ', type(student)) # student Data type of  <class 'set'>

Basic operations of collection

1. Add Elements

add()

Functions:

Used to add 1 element to the collection. If the element already exists in the collection, the function does not execute

Usage:


set.add(item)
 Parameters: 
item: Elements to add to the collection 

a_list = ['python', 'django', 'django', 'flask']
a_set = set()
a_set.add(a_list[0])
a_set.add(a_list[1])
a_set.add(a_list[2])
a_set.add(a_list[-1])
print(a_set) # {'flask', 'django', 'python'}
#  Duplicate elements are not added to the collection 
a_set.add(True)
a_set.add(None)
print(a_set) # {True, None, 'django', 'python', 'flask'}
#  Collections are unordered 

From the above example, it can be proved that:

1. A collection is a sequence of non-repeating elements

2. Collections are unordered

update()

Functions:

Add a new set (list, element, string), ignoring the elements in symplectic geometry if they exist in the original set

Usage:


set.update(iterable)
 Parameters: 
iterable Collections, lists, tuples, strings 

# update
a_tuple = ('a', 'b', 'c')
a_set.update(a_tuple)
print(a_set) # {True, None, 'a', 'django', 'c', 'flask', 'b', 'python'}
a_set.update('python')
print(a_set) # {True, 'o', 't', None, 'h', 'a', 'django', 'c', 'flask', 'y', 'n', 'b', 'python', 'p'}

2. Remove Elements

remove()

Functions:

Delete an element from the collection. If the element does not exist, an error will be reported

Methods:


set.remove(item)
 Parameters: 
iten: Object in the current collection 1 Elements 

clear()

Functions:

Empty all elements in the current collection

Usage:


set.remove(item)
 Parameters: 
iten: Object in the current collection 1 Elements 

Important note:

Collection cannot get elements by index Collection cannot get any methods of elements Collection is only a temporary type used to handle lists or tuples, which is not suitable for storage and transmission

a_set.remove('python')
print(a_set) # {'p', True, None, 'y', 'a', 't', 'o', 'flask', 'n', 'b', 'h', 'django', 'c'}
a_set.clear()
print(a_set) # set()
a_set.remove('django') # KeyError: 'django'

3. Intersection of sets

What is intersection?

a and b have the same set of elements, which is called the intersection of a and b

intersection()

Functions:

Returns the elements contained in two or more collections, that is, the intersection

Usage:


a_set.intersection(b_set...)
 Parameters: 
b_set...:  Object compared to the current collection 1 Set or sets 
 Return value: 
	 Returns the intersection of the original collection and the contrast collection 

a = ['dewei', 'xiaomu', 'xiaohua', 'xiaoguo']
b = ['xiaohua', 'dewei', 'xiaoman', 'xiaolin']
c = ['xiaoguang', 'xiobai', 'dewei', 'xiaooyuan']
a_set = set(a)
b_set = set(b)
c_set = set(c)
print(a_set, b_set, c_set)
result = a_set.intersection(b_set, c_set)
xiaotou = list(result)
print('{} It's this thief '.format(xiaotou[0]))

3. Union of sets

What is union? The elements of a and b are the union of a and b

union()

Functions:

Returns the union of multiple sets, that is, the elements containing all sets, and the repeated elements appear once

Usage:


set.add(item)
 Parameters: 
item: Elements to add to the collection 
0

set.add(item)
 Parameters: 
item: Elements to add to the collection 
1

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: