当前位置:网站首页>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():
代码块
边栏推荐
猜你喜欢

【ASM】字节码操作 MethodVisitor 案例实战 生成对象
![[Machine Learning] Detailed explanation of web crawler combat](/img/ac/f00f0c81e66ba526ac39ee60fad72b.png)
[Machine Learning] Detailed explanation of web crawler combat

多线程案例——阻塞式队列
Ontology Development Diary 03 - When debugging is in progress

BigDecimal用法常用操作记录

ArrayList和LinkedList

Tom Morgan | 人生二十一条法则

Ontology development diary 04 - to try to understand some aspects of protege

自定义类型:结构体,枚举,联合

文件操作
随机推荐
6.File类
梦笔记0809
记录一次被入侵5900端口经历
常用的一些制表符号
Cisco common basic configuration of common commands
【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
喜迎排名18
Browser error classification
如何快速打通镜像发布流程?
mysql 修改密码和忘记密码操作
[Personal study summary] CRC verification principle and implementation
自定义类型:结构体,枚举,联合
多线程(基础)
MySQL常用存储引擎,你不可错过的知识点!
Dream Notes 0809
浏览器的报错分类
goproxy.io 证书过期
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
缓存击穿,缓存穿透,缓存雪崩的解释和对应的一些解决方案
技术分享 | 如何模拟真实使用场景?mock 技术来帮你