当前位置:网站首页>Collections and dictionaries
Collections and dictionaries
2022-06-26 04:56:00 【The story of Ula】
Collections and dictionaries
List of articles
One 、 aggregate (set):
Definition :
- 1. Composed of different elements
- 2. Is an unordered, non repeating sequence of elements bi-
- 3. The element of the collection must be of immutable type
It can be used{}
orset()
Function to create a collection , Be careful : To create an empty collection, you must useset()
instead of{}
, because{}
Is used to create an empty dictionary .
1. Create format :
par = {
value1,value2,value3,...}
perhaps
set(value)
Add elements to the collection
s.add(a)#a If a Already exist , No operations
perhaps
s.update(a)#a Can be a list , Tuples , Dictionary, etc .
a It can be more than one , Separated by commas .
2. Remove elements :
s.remove(a)# take a Remove from collection , If the element does not exist , Will report a mistake .
s.discard(a)# take a From the collection s Remove from , If the element does not exist , And there will be no mistakes
s.pop()# Randomly delete an element in the collection .
set A collection of pop Method will sort the collection out of order , Then delete the first element on the left of the unordered set .
3. Calculate collection elements :
len(s)# Computing sets s The number of elements in
4. Empty the set :
s.close()# Empty the set s
5. Determine whether the element exists in the collection :
a in s# Element of judgement a Is it gathering s in , There is returned True, There is no return False.
6. Methods built into the collection
Method | describe |
---|---|
add() | Add an element to the collection |
clear() | Remove all elements from the collection |
copy() | Copy a collection |
difference() | Returns the difference set of multiple sets |
difference update() | Remove elements from collection , This element also exists in the specified collection |
discard() | Delete the elements specified in the collection |
intersection() | Returns the intersection of sets |
intersection update() | Returns the intersection of sets |
isdisjoint() | Determine whether two sets contain the same elements , No return True, Otherwise return to False |
issubset() | Determine whether the specified set is a subset of the method parameter set |
issuperset() | Judge whether the method parameter set is a subset of the specified set |
pop() | Remove elements at random |
remove() | Removes the specified element |
symmetric difference() | Returns the set of elements that are not repeated in two sets |
symmetric difference update() | Remove the same elements from the current collection in another specified collection , And insert different elements of another specified collection into the current collection |
union() | Returns the union of two sets |
update() | Crisis and adding elements |
Two 、 Dictionaries (dictionary):
- Python Built in dictionary :dict Support for ,dict The entire dictionary. Dictionary is another variable container model , And can store any type of object .
- Dictionaries are extremely fast to find .
- The key must be unique , Value doesn't have to be
- Values can be any type , The key must be immutable
- len(): Return dictionary key : The number of value pairs .
- d[k]: Return the value corresponding to the keyword .
- d[k] = v: Associate values to key values k On .
- del d[k]: The delete key value is k The item .
- key in d: Key value key Whether in d in , Is to return True, Otherwise return to False
1. The basic format of the dictionary :
d = {
key1:value1,key2:value2}
Key to dictionary (key) Must be immutable , value (key) It can be any data type . The dictionary is out of order , Print every time , The order will change
2. Dictionary operation :
len(dict)
: Count the number of dictionary elements , That is, the total number of .str(dict)
: Output Dictionary , In a printable string .type(variable)
: Returns the type of variable entered , If the variable is a dictionary , Return the dictionary type .
3. Dictionary method :
del(key)
: Delete elements from Dictionary .clear()
: Empty dictionary .del Dictionary name
: Delete Dictionary .iteams()
: Loop through the dictionary key and value.keys()
: Loop through all the in the dictionary key.values()
: Loop through all the in the dictionary value.copy()
: Shallow copy fromkeys()
: Create a new dictionary ,fromkeys(*args,**kwargs) With *args Make a dictionary key ,**kwargs Is the corresponding initial value of all keys .pop()
: Delete specified key.popitem()
: Randomly delete a key value pair .setdefault()
: Add a new key to the dictionary , There is no execution , If it doesn't exist, increase and return the value corresponding to the key .update()
: Update Dictionary .
边栏推荐
- Solution to back-off restarting failed container
- ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
- File upload and security dog
- Numpy random number
- Image translation /gan:unsupervised image-to-image translation with self attention networks
- Selection of programming language
- 图解OneFlow的学习率调整策略
- 文件上传与安全狗
- One of token passing between microservices @feign's token passing
- ROS notes (07) - Implementation of client and server
猜你喜欢
2.< tag-动态规划和常规问题>lt.343. 整数拆分
图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译
Final review of brain and cognitive science
2. < tag dynamic programming and conventional problems > lt.343 integer partition
记录一次循环引用的问题
NVM installation and use and NPM package installation failure record
Rsync common error messages (common errors on the window)
2.22.2.14
Multipass中文文档-远程使用Multipass
【Latex】错误类型总结(持更)
随机推荐
2022.1.23
基础查询
ROS notes (07) - Implementation of client and server
Solution to back-off restarting failed container
1.13 learning summary
Multipass中文文档-移除实例
Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond
Hash problem
2022.2.16
2.9 learning summary
1.20 learning summary
numpy 数据输入输出
Rsync common error messages (common errors on the window)
Illustration of ONEFLOW's learning rate adjustment strategy
Motivational skills for achieving goals
File upload and security dog
A company crawling out of its grave
202.2.9
1.16 learning summary
Multipass中文文档-使用Packer打包Multipass镜像