Full stack growth - data type of python study notes - collection

gather

1. The difference between a collection and a list
Function the list gather
symbol (definition) [1,2,3,4] {1,2,3,4}
content can be repeated not repeatable
Function For the use of data Used for data union, intersection, and difference acquisition
index indexed no index
order orderly out of order

定义一个集合

a_set = new set() #定义一个set
set(list) #将列表转为集合
set(dict) #将字典转为集合
2. Common methods of collection
method name usage
add(item) Adds an element to the collection and does not execute if it exists. The method has no return value
update() Add a new collection (or list, tuple, string) and ignore set.update(list||str) if it exists in the original collection
remove(item) Remove the element that is item in the collection. If the item does not exist in the collection, an error will be reported
clear() Clear the collection directly to be empty
difference() set1.difference(set2) returns the difference between set1 and set2 {1,2,3,4}.difference({3,4,5,6}) returns {1,2,5,6}
intersection() set1.intersection(set2) returns the intersection {1,2,3} of set1 and set2.intersection{3,4,5} returns {3}
union() set1.union(set2) returns the union of sets1 and set2 {1,2,3,4}.union({3,4,5,6}) returns {1,2,3,4,5,6}
isdisjoint() set1.isdisjoint(set2) returns a Boolean value and returns True if neither set2 nor set1 are duplicates
3.PS
  • Collection cannot get element by index
  • Collections don't have any methods for getting elements
  • Collection is only a temporary type used to process lists or tuples, it is not suitable for data storage and transmission

Guess you like

Origin blog.csdn.net/qq_51075057/article/details/130505123