Home  >  Article  >  Backend Development  >  Python zero-based introduction to ten collections

Python zero-based introduction to ten collections

黄舟
黄舟Original
2017-01-16 14:26:301279browse

First remember several characteristics of sets: sets are unordered, and the elements in the set are unique.
1. The definition of a set
The definition of a set is very similar to a dictionary, except that the key-value pairs in the dictionary are converted into elements.

num={1,2,3,4,5}
print(type(num))
print(num)
print("\n")
#集合会自动剔除重复的数据,并且集合也是无序的
num2={1,2,3,4,5,5,4,32}
print(num2)
print("\n")

Python zero-based introduction to ten collections

#2. Creation of collections
(1) Use the definition method
(2) Use the set() factory function

set1=set([1,2,3,4,5,6])
print(set1)

Python zero-based introduction to ten collections

3. Commonly used functions of collections
(1)add() function: Add functions to the collection
(2)remove() function: Remove elements from the collection
( 3) Use frozenset() to create an unmodifiable collection: Once a collection is created, the collection cannot be modified, otherwise an exception will occur

num3=frozenset([1,2,3,4,5])
num3.add(6)

Python zero-based introduction to ten collections

The above is a zero-based introduction to Python Ten collections of content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


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