当前位置:网站首页>tuple dictionary collection
tuple dictionary collection
2022-08-09 09:58:00 【Wu Xizhong】
一.元组
1.A basic introduction to tuples
1.1 The representation of the tupletuple
1.2Methods to create tuples:tuple()
tu = tuple()
1.3 元组是一个不可变序列
1.4 If the tuple is not empty include at least one comma,当元组不是空元组时括号可以省略
1.5 元组的解包:Assign each element in the tuple to a variable.
a,b,c = 1,2,3
print('a=',a)
print('b=',b)
print('c=',c)
二.Introduction to Dictionary
1.字典的介绍:Dictionaries are a new data type,简称映射(mapping)
2.字典的特点:
2.1 Dictionaries have poor storage performance compared to lists,But the query performance is good,The storage performance of the list is good,But the query performance is relatively poor.
2.2 The keys in the dictionary are unique and cannot be repeated,If there is a repetition, the latter will replace the former
2.3 字典的值可以是任意的对象,The keys of a dictionary can be any immutable object
2.4 Dictionaries work similarly to lists,都是用来存储对象的容器.
3.字典的使用方法
3.1 dict()This function creates a dictionary
方法一:
dr = dict(name='葫芦娃',age=18,gender='female')
方法二:
dr = dict([('name','葫芦娃'),('age',18),'ab',('gender','female')])
3.2 get(key[,default]) 根据键来获取字典的值.第二个参数可以指定一个默认值,当获取不到值的时候会返回默认值
dr = dict([('name','葫芦娃'),('age',18),'ab',('gender','female')])
r = dr.get('classmate','boy')
3.3 通过keyto modify the corresponding in the dictionaryvaluevalue or add to the dictionarykey-value键值对
3.3.1 如果字典中有对应的key,Then modify the dictionarykey对应的value值
dr = dict([('name','葫芦娃'),('age',18),'ab',('gender','female')])
dr['name'] ='小朋友’
print(dr)
输出结果:
{
'name':'小朋友','age':18,'gender':'female'}
3.3.2 如果字典中没有对应的key,It means adding a new one to the dictionarykey-value键值对
dr = dict([('name','葫芦娃'),('age',18),'ab',('gender','female')])
dr['classmate'] ='男生’
print(dr)
输出结果:
{
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
3.4 update() 将其他字典的key-value添加到当前的字典当中
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
dr_one = {
'birthday':20200909,'style':'deli'}
dr.update(dr_one)
print(dr)
输出结果:
{
'name': '葫芦娃', 'age': 18, 'gender': 'female', 'classmate': '男生', 'birthday': 20200909, 'style': 'deli'}
3.5 del 删除字典中的key-value
3.6 popitem()Randomly delete from the dictionarykey-value键值对,Usually the last one in the dictionary is removed,该方法有返回值,The return value is deletedkey-vakueA tuple of key-value pairs
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
r = dr.popitem()
print(r)
print(dr)
输出结果:
('classmate', '男生')
{
'name': '葫芦娃', 'age': 18, 'gender': 'female'}
3.7 pop(key[,default]) 根据key删除指定的value,This method also has a return value,What is returned is deletedkey对应的value.第二个参数可以指定一个默认值,当获取不到值的时候会返回默认值
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
dr.pop('name')
print(r)
print(dr)
输出结果:
葫芦娃
{
'age': 18, 'gender': 'female', 'classmate': '男生'}
3.8 setdefault(key,value) :When there is already a corresponding one in the dictionarykey的名字时,This method does nothing with dictionaries,When it is not in the dictionarykeyof the same name,will add the specified to the dictionarykey-value键值对
dr = dict([('name','葫芦娃'),('age',18),'ab',('gender','female')])
dr.setdefault('name','hello')
print(dr)
dr.setdefault('seion','hay')
print(dr)
输出结果:
{
'name': '葫芦娃', 'age': 18, 'a': 'b', 'gender': 'female'}
{
'name': '葫芦娃', 'age': 18, 'a': 'b', 'gender': 'female', 'seion': 'hay'}
三.浅复制与深复制(This method doesn't just work with dictionaries,Also works for lists etc)
1.copy() method is used to make a shallow copy of the dictionary
注意:A shallow copy will only copy the dictionary itself,如果字典中还有个字典是不会进行复制的
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
d = dr.copy()
print(d)
输出结果:
{
'name': '葫芦娃', 'age': 18, 'gender': 'female', 'classmate': '男生'}
2.deepcopy()Used to make deep copies of dictionaries
注意:A deep copy doesn't just copy the dictionary itself,If there is another dictionary in the dictionary, it can also be copied
import copy
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生','li':[1,2,3,4]}
d = copy.deepcopy(dr)
d['li'][0] = 'hello'
print(d)
print(dr)
输出结果:
{
'name': '葫芦娃', 'age': 18, 'gender': 'female', 'classmate': '男生', 'li': ['hello', 2, 3, 4]}
{
'name': '葫芦娃', 'age': 18, 'gender': 'female', 'classmate': '男生', 'li': [1, 2, 3, 4]}
四.遍历字典
1.keys() This method returns all of the dictionarykey
语法:
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
for key in dr.keys():
代码块
2.values() 该方法返回一个序列 The sequence holds the values of the dictionary
语法:
```python
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
for value in dr.values():
代码块
3.items() 该方法会返回字典中所有的项 It returns a sequence,序列中包含有双值子序列 双值分别是 字典中的key和value
语法:
dr = {
'name':'葫芦娃','age':18,'gender':'female','classmate':'男生'}
for i in dr.items():
代码块
边栏推荐
- 7.Collections tool class
- LeetCode148:排序链表 归并排序,思路清晰,C语言练习看过来!
- 主从postition变化无法锁定_Slave_IO_Running显示No_Slave_Sql_Running显示No---Mysql主从复制同步002
- Arrays类、冒泡排序、选择排序、插入排序、稀疏数组!
- mysql 修改密码和忘记密码操作
- 喜迎排名18
- ArrayList和LinkedList
- [Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
- MySQL约束关系,你必须要知道的知识点!
- 1. The concept of flow
猜你喜欢
安装torch_sparse失败解决方法
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
EndNote使用指南
Redis 回击 Dragonfly:13 年后,Redis 的架构依然是同类最佳
时间复杂度和空间复杂度
动态内存管理
.ts 音频文件转换成 .mp3 文件
字符串函数和内存函数
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
[Personal study summary] CRC verification principle and implementation
随机推荐
安装torch_sparse失败解决方法
元组 字典 集合
实用小技能:一键获取Harbor中镜像信息,快捷查询镜像
蓄电池建模、分析与优化(Matlab代码实现)
技术分享 | 使用 cURL 发送请求
Ontology development diary 02 - simple sparql query
基于信号量与环形队列实现读写异步缓存队列
[ASM] Bytecode operation MethodVisitor case combat generation object
4. Generics and Utilities
3.编码方式
4.泛型和工具类
Go-控制语句那些事
Practical skills: a key for image information in the Harbor, quick query image
《刷题日记》2
拿下跨界C1轮投资,本土Tier 1高阶智能驾驶系统迅速“出圈”
Celebrate ranked 18
6.Map interface and implementation class
1.线程简介
MySQL关于表的知识点,进来学习!
cannot import name ‘load_offloaded_weights‘ from ‘accelerate.utils‘ (/home/huhao/anaconda3/envs/huha