当前位置:网站首页>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():
代码块
边栏推荐
猜你喜欢
Practical skills: a key for image information in the Harbor, quick query image
《刷题日记》2
通过程序发送 Gmail 邮件
【八大排序②】选择排序(选择排序,堆排序)
【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
Command line query database
2021-04-26QGIS3.10加载天地图影像(地图瓦片)的一种方法
Cisco common basic configuration of common commands
Quick sort eight sorts (3) 】 【 (dynamic figure deduction Hoare, digging holes, front and rear pointer method)
随机推荐
6. The File types
Go-控制语句那些事
2.Collection interface
Arrays.toString( )打印二维数组
KeyBERT和labse提取字符串中的关键词
6.Map interface and implementation class
3.List interface and implementation class
Multi-threaded cases - timer
MySQL约束关系,你必须要知道的知识点!
5. Transform Streams
Go-指针的那些事
Tom Morgan | 人生二十一条法则
动态内存管理
4.字符流
喜迎排名18
需求侧电力负荷预测(Matlab代码实现)
pycharm在创建py文件时如何自动注释
安装torch_sparse失败解决方法
5.转换流
文件操作