Python核心编程之基础课 07元组&字典&集合

7.1 元组

7.1.1 元组基本介绍

  • 元组表现形式 tuple
# ### 创建一个空元组
# null_tuple = ()
# print(null_tuple,type(null_tuple),len(null_tuple))  ##() <class 'tuple'> 0
  • 特点
    • 是一个不可变序列
### 能否修改? ---> 不能! TypeError: 'tuple' object does not support item assignment
# my_tuple = (1,2,3,4,5)
# my_tuple[3] = 6
# print(my_tuple[3])   ###TypeError: 'tuple' object does not support item assignment
  • 不是空元组时,至少有一个逗号(,)
  • 不是空元组时,()可以省略
# my_tuple = 10
# print(my_tuple,type(my_tuple))    ###10 <class 'int'>
# ###如果元组不是空的,它里面至少得有一个逗号
# my_tuple = 10,
# print(my_tuple,type(my_tuple))    ###(10,) <class 'tuple'>
# my_tuple = 1,2,3,4,               ###(1, 2, 3, 4) <class 'tuple'>
# print(my_tuple,type(my_tuple))
  • 应用场景
    当希望数据不改变时,使用元组。
    其它场景下基本都用列表。

7.1.2 元组的使用

  • 取值(索引)
my_tuple = (1,2,3,4,5)
# print(my_tuple,type(my_tuple))  ##(1, 2, 3, 4, 5) <class 'tuple'>
# ##取值
# print(my_tuple[1])    ###2
  • 元组解包(*的使用)
    将元组当中的元素赋值给一个个变量。
    注意:原则上解包时变量的数量,要和元组的元素一一对应;当不对应时,可在变量前加上*号,这样变量将会获取元素当中剩余的元素,以列表的形式返回。
### 元组的解包:将元组当中的元素赋值给一个个变量
### 注意:原则上解包时变量的数量,要和元组的元素一一对应;
### 当不对应时,可在变量前加上*号,这样变量将会获取元素当中剩余的元素,以列表的形式返回。
my_tuple = 10,20,30,40,
# a,b,c,d = my_tuple
# print("a = ", a,type(a))    ###a =  10 <class 'int'>
# print("b = ", b,type(b))    ###b =  20 <class 'int'>
# print("c = ", c,type(c))    ###c =  30 <class 'int'>
# print("d = ", d,type(d))    ###d =  40 <class 'int'>

###ValueError: too many values to unpack (expected 2)
# a,b = my_tuple
# print("a = ", a,type(a))  ###ValueError: too many values to unpack (expected 2)
# print("b = ", b,type(b))

# a,*b = my_tuple
# print("a = ", a,type(a))    ###10 <class 'int'>
# print("b = ", b,type(b))    ###[20, 30, 40] <class 'list'>
# ###SyntaxError: invalid syntax
# a,b* = my_tuple
# print("a = ", a,type(a))
# print("b = ", b,type(b))

# ###SyntaxError: invalid syntax
# a*,b = my_tuple
# print("a = ", a,type(a))
# print("b = ", b,type(b))
# *a,b = my_tuple
# print("a = ", a,type(a))    ###[10, 20, 30] <class 'list'>
# print("b = ", b,type(b))    ###40 <class 'int'>

# a,b,*c = my_tuple
# print("a = ", a,type(a))    ###10 <class 'int'>
# print("b = ", b,type(b))    ###20 <class 'int'>
# print("c = ", c,type(c))    ###[30, 40] <class 'list'>

a,*b,c = my_tuple
print("a = ", a,type(a))    ###10 <class 'int'>
print("b = ", b,type(b))    ###[20, 30] <class 'list'>
print("b = ", a,type(b))    ###10 <class 'list'>

7.2 字典

7.2.1 字典的基本介绍

  • 属于一种新的数据结构,称为映射(mapping)
  • 创建一个有数据的字典,语法:{key:value}
### 字典:dict
### 创建一个有数据的字典,语法:{key:value}
# dct = {"name":"葫芦娃","age":"7","gender":"男"}
# print(dct,type(dct))    ####{'name': '葫芦娃', 'age': '7', 'gender': '男'} <class 'dict'>
  • 字典作用和列表类似,都是用来存储对象的容器
    列表存储数据的性能好,但查询数据的性能差;字典与之相反。
  • 在字典中每一个元素都有唯一的名字,通过这个唯一的名字可以找到指定的元素
  • 这个唯一的名字我们称之为key,通过key可以快速查找value,称之为值。
    字典也称之为:键值对[key:value]结构。
  • 每个字典可以有多个键值对,每一个键值对,称其为一项(Item)
dct = {"name":"葫芦娃","age":"7","gender":"男"}
### 获取值:根据键(key)来获取值(value)
print(dct['name'],dct['age'],dct['gender'])    ###葫芦娃 7 男
  • 字典的值可以是任意对象,字典的键可以是任意的不可变对象(int,str,bool,tuple…)
test_Tuple = ("test","tupleTest")
dct = {1:{"numList":[1,2,3]},"str":"(4,5,6)",test_Tuple:("test1","test2")}
print(dct)    ### {1: {'numList': [1, 2, 3]}, 'str': '(4,5,6)', ('test', 'tupleTest'): ('test1', 'test2')}
print(dct[1]["numList"][2])             ### 3
print(dct["str"][1],type(dct["str"][2]))         ### 4 <class 'str'>
print(dct[test_Tuple][1],type(dct[test_Tuple][1]))    ### test2 <class 'str'>
  • 字典的键是不能重复的。如果出现重复的,则会替换前面的值。
# ### 字典的键不能重复。如果出现重复,后面的会覆盖(替换)前面的值
# dct = {"name":"葫芦娃","age":"7","gender":"男","name":"霸王花"}
# print(dct,type(dct))    ###{'name': '霸王花', 'age': '7', 'gender': '男'} <class 'dict'>

7.2.2 字典的使用

  • dict()函数创建字典
    关键字=XXX。注意:关键字不带引号!
    dict也可以将一个包含有双值子序列的序列,转换为字典。
### 使用dict()函数来创建字典:关键字=XXX,
### 注意:关键字不带引号!
# dct = dict(name="葫芦娃", age = 7, gender="男")
# print(dct,type(dct))    ###{'name': '葫芦娃', 'age': 7, 'gender': '男'}  <class 'dict'>

### dict也可以将一个包含有双值子序列的序列,转换为字典。
### 双值序列:序列当中有2个值[4,5],(1,2),'ab'
### 子序列:如果序列当中的元素也是序列,称这个元素为子序列[(1,2),(4,5)]
### 双值子序列:应用场景较少,了解即可。
# dct = dict([("name","葫芦娃"), ("age","7"), ("gender","男")])
# print(dct,type(dct))    ###{'name': '葫芦娃', 'age': 7, 'gender': '男'}  <class 'dict'>
  • len():字典中键值对的个数
# ### len():字典中键值对的个数
# print(len(dct))    ### 3
  • in (not in):检查字典中是否(不)包含有指定的键
# ### in:检查字典中是否包含有指定的键。
# ### not in: 检查字典中是否不包含指定的键。
# print('name' in dct)    ### True
# print('葫芦娃' in dct)    ### False
# print('7' in dct)    ### False
# print('男' not in dct)   ### True

# ### 注意直接引用键,需要加引号
# print(dct[name], dct['age'], dct['gender'])    ###不加会报语法错误:NameError: name 'name' is not defined
# print(dct['name'], dct['age'], dct['gender'])    ###葫芦娃 7 男

###这种情况下不加
# name2 = 'name'
# # print(dct['name2'])    ### KeyError: 'name2'
# print(dct[name2])    ###葫芦娃
  • get(key[.default])
    根据键来获取字典的值。第二个参数可以指定一个默认值,当获取不到值时,返回这个默认值。
# ###get(key[,default])通过get方法也可以获得字典当中的值value, 不会报错。
# print(dct.get("name"))    ### 葫芦娃
# print(dct.get("name","我也不知道"))    ### 葫芦娃
# print(dct.get("name2"))    ### None
# print(dct.get("name2","没有这个键"))    ### 没有这个键
  • 字典的修改
    修改已存在的键对应的值:修改字典中的value;
    如果字典中没有Key,则向字典中添加key:value
# ### 字典的修改:
# ### 1)修改已存在的键对应的值:修改字典中的value
# dct = {'name': '葫芦娃', 'age': 7, 'gender': '男'}
# dct['name'] = "霸王花"
# print(dct)    ###{'name': '霸王花', 'age': 7, 'gender': '男'}
# print(dct["name"])    ###霸王花
# ### 2)新增一个键值对。如果字典中没有Key,则向字典中添加key:value.
# dct['phoneType'] = "Huawei Nova3i"    ###{'name': '葫芦娃', 'age': 7, 'gender': '男', 'phone': 'Huawei Nova3i'}
  • setdefault(key[,default]),向字典中添中key-value
    如果字典中已存在key值,不会对字典有任何的操作(影响),原字典不变。
    如果字典中的key值不存在,则向这个字典中添加这个key,并设置value,且有返回值
# ### setdefault(key[,default]),向字典中添中key-value
# ### 如果字典中已存在key值,不会对字典有任何的操作(影响),原字典不变。
# ### 如果字典中的key值不存在,则向这个字典中添加这个key,并设置value
# ### 且有返回值
# result = dct.setdefault("name","霸王花")
# print(result,type(result)) ### 葫芦娃 <class 'str'>
# print(dct)    ### {'name': '葫芦娃', 'age': 7, 'gender': '男'}
#
# result = dct.setdefault("nameNew","霸王花")
# print(result,type(result))    ###霸王花 <class 'str'>
# print(dct)    ### {'name': '葫芦娃', 'age': 7, 'gender': '男', 'nameNew': '霸王花'}
  • update():
    将其它字典中的key-value添加到当前的字典中。如果有重复的key值,则后面的会覆盖前面的值,如果存在相同的key值,则会覆盖老的值
# # ### update()方法:将其它字典中的key-value添加到当前的字典中。
# # ### 如果有重复的key值,则后面的会覆盖前面的值
# dct = {'a': '1', 'b': 2, 'c': '3'}
# dct2 = {'d': '4', 'e': 5, 'f': '6'}
# dct.update(dct2)    ####将dct2中的key-value添加到当前的字典dct中
# print(dct)    ### {'a': '1', 'b': 2, 'c': '3', 'd': '4', 'e': 5, 'f': '6'}
# print(dct2)   ### {'d': '4', 'e': 5, 'f': '6'}

# ### 如果存在相同的key值,则会覆盖老的值
dct = {'a': '1', 'b': 2, 'c': '3'}
dct2 = {'d': '4', 'e': 5, 'a': '6'}
dct.update(dct2)    ### {'a': '6', 'b': 2, 'c': '3', 'd': '4', 'e': 5}
  • del 关键字来删除字典中的key-value值
# ### 删除,可以用:del关键字来删除字典中的key-value值
del dct['a']
del dct['b']
print(dct)    ###{'c': '3', 'd': '4', 'e': 5}
  • popitem()
    随机删除字典中的一个key-value对,一般都会删除最后一个;
    删除之后会将删除的key-value作为返回值返回。
    返回类型:元组。元组中有2个元素,第一个元素是删除掉的key,第二个元素是删除的value
# ### popitem()随机删除字典中的一个key-value对,一般都会删除最后一个
# ### 删除之后会将删除的key-value作为返回值返回。
# ### 返回类型:元组。元组中有2个元素,第一个元素是删除掉的key,第二个元素是删除的value
# result = dct.popitem()
# print(result)    ### ('e', 5)
# print(dct)    ### {'c': '3', 'd': '4',}
#
# result = dct.popitem()
# print(result)    ### ('d', '4')
# print(dct)     ###{'c': '3'}
  • pop(key[,default]
    根据key来删除字典中的key-value,当指定的key不存在时,对原字典没有任何影响
# # ### pop()方法:根据key来删除字典中的key-value.
# result = dct.pop("d")
# print(result)    ### 4
# print(dct)     ### {'c': '3', 'e': 5}
# ###当指定的key不存在时,对原字典没有任何影响
# result = dct.pop("KeyError")    ### KeyError: 'Notexist'
# print(result)     ###
# result = dct.pop("Notexist","这个Key没有")
# print(result)    ### 这个Key没有
# print(dct)    ### {'c': '3', 'd': '4', 'e': 5}
# ### pop灵活些
  • copy()方法,用于对字典进行浅复制。
    注意:= 不是复制,是赋值,指向同一个对象。
    若值是字典形式,则原件和copy件同时改变;
    其它情况下一方改变了,另一方不受其影响。
# ### 1) 浅复制
# ### copy()方法,用于对字典进行浅复制
# dct = {'a': '1', 'b': 2, 'c': '3'}
# ###注意该方式不是复制,是赋值,指向同一个对象。
# dct2 = dct
# # ### Result:二者值、ID一模一样
# print("dct = ",dct, id(dct))
# print("dct2 = ",dct2,id(dct2))
#
# # ### Result:二者值、ID一模一样
# dct["b"] = 444
# print("dct = ",dct,id(dct))
# print("dct2 = ",dct2,id(dct2))

# ### 以下是复制copy
# dct = {'a': '1', 'b': 2, 'c': '3'}
# dct2 = dct.copy()
# # ### Result一致,id不同。二者没有任何关系。
# print("dct = ",dct,id(dct))
# print("dct2 = ",dct2,id(dct2))
#
# # ### Result:dct的值变了,dct2没有跟着变
# dct["b"] = 444
# print("dct = ",dct,id(dct))     ### {'a': '1', 'b': 444, 'c': '3'}
# print("dct2 = ",dct2,id(dct2))   ### {'a': '1', 'b': 2, 'c': '3'}

# dct = {'a': {"name":"黑猫警长"}, 'b': 2, 'c': '3'}
# dct2 = dct.copy()
# # ### Result:二者值一致: {'a': {'name': '黑猫警长'}, 'b': 2, 'c': '3'}
# print("dct = ",dct)
# print("dct2 = ",dct2)

# ### 若值是字典形式,则同时变
# dct["a"]["name"] = "霸王花"
# # ### Result:二者值一致!{'a': {'name': '霸王花'}, 'b': 2, 'c': '3'}
# print("dct = ",dct)
# print("dct2 = ",dct2)

# dct2["a"]["name"] = "黑寡妇"
# # ### Result:二者值一致!{'a': {'name': '黑寡妇'}, 'b': 2, 'c': '3'}
# print("dct = ",dct)
# print("dct2 = ",dct2)

# ### 应用场景:爬虫
  • deepcopy():深复制
    需要引用copy模块。二者之间修改无任何关联,一方改变,丝毫不影响另一方
# ### 深复制需要引用一个模块
import copy
# ### 2).deepcopy深copy
# ### Result:某一个改变了,不影响另一个。
dct = {'a': {'name': '黑猫警长'}, 'b': 2, 'c': '3'}
dct2 = copy.deepcopy(dct)
print("dct = ",dct)      ### {'a': {'name': '黑猫警长'}, 'b': 2, 'c': '3'}
print("dct2 = ",dct2)    ### {'a': {'name': '黑猫警长'}, 'b': 2, 'c': '3'}

# dct2["a"]["name"] = "黑寡妇"
# print("dct = ",dct)      ### 不变 {'a': {'name': '黑猫警长'}, 'b': 2, 'c': '3'}
# print("dct2 = ",dct2)   ### 改变了 {'a': {'name': '黑寡妇'}, 'b': 2, 'c': '3'}

dct["a"]["name"] = "娃哈哈"
print("dct = ",dct)    ###变了 {'a': {'name': '娃哈哈'}, 'b': 2, 'c': '3'}
print("dct2 = ",dct2)  ###不变 {'a': {'name': '黑猫警长'}, 'b': 2, 'c': '3'}
  • clear():清空字典
# ### clear():清空字典
dct.clear()
print(dct)    ### {}

7.2.3 遍历字典

可以通过以下3种方式对字典进行遍历:

  1. keys() 返回字典所有的key
dct = {"name":"葫芦娃","age":"7","gender":"男"}
print(dct.keys())    ### dict_keys(['name', 'age', 'gender'])
for key in dct.keys():
    print(dct[key],end=" ")    ### 葫芦娃 7 男
  1. values(): 返回一个序列,序列中保存有字典的值
dct = {"name":"葫芦娃","age":"7","gender":"男"}
print(dct.values())        ### dict_values(['葫芦娃', '7', '男'])
for value in dct.values():
    print(value,end=" ")   ### 葫芦娃 7 男
  1. items(): 返回字典中所有的项,返回一个序列,序列中包含有双值子序列,双值分别是:字典中的key和value
dct = {"name":"葫芦娃","age":"7","gender":"男"}
print(dct.items())    ### dict_items([('name', '葫芦娃'), ('age', '7'), ('gender', '男')])
  1. 分别获取key,value
"""
name <class 'str'>
葫芦娃 <class 'str'>
age <class 'str'>
7 <class 'str'>
gender <class 'str'>
男 <class 'str'>
"""
### 分别获取key,value
dct = {"name":"葫芦娃","age":"7","gender":"男"}
for key,value in dct.items():
    print(key,type(key))
    print(value,type(value))

7.3 集合

7.3.1 集合简介

集合的表现形式:set,和列表非常相似。

7.3.2 集合与列表的区别

  • 集合只能存储不可变对象
  • 集合中存储的对象是无序的,不是按照元素的插入顺序保存的
  • 集合中不能出现重复元素

7.3.3 集合的常用操作

  1. 验证
  • 集合的创建,无序存储,不是按照插入顺序存储的
###集合的创建,无序存储,不是按照插入顺序存储的。
s = {100,10,1,2,3,4}
print(s,type(s))    ### 可能结果 {1, 2, 3, 100, 4, 10} <class 'set'>

# s= {}    ### 默认字典
# print(s,type(s))   ### {} <class 'dict'>

### 集合的创建
s = set()    ### set函数创建空集合
print(s,type(s))    ### set() <class 'set'>
  • 集合中不能出现重复元素
### 集合中不能出现重复元素
s = {100,10,1,2,3,4,1,100,4}
print(s,type(s))  ### 顺序不定,可能结果, {1, 2, 3, 100, 4, 10} <class 'set'>
  • 整数、小数、元组、字符串不可变;列表是可变序列
# ### 集合只能存储不可变对象
# s = {[1,2,3],[4,5,6]}    ### TypeError: unhashable type: 'list'
# print(s,type(s))
  1. set()函数只能传递1个参数,且参数是可迭代的,如:列表
# #### set()函数只能传递1个参数,且参数是可迭代的,如:列表
# #s = set(1,2,3)
# #print(s,type(s))    ### TypeError: set expected at most 1 argument, got 3
# #s = set(1)
# #print(s,type(s))    ### TypeError: 'int' object is not iterable
  1. 通过set()将序列和字典转换成集合
### set函数可以将一个序列变成一个集合
s = set([1,2,3,4,5,3])
print(s,type(s))    ### {1, 2, 3, 4, 5} <class 'set'>
s = set("hello")
print(s,type(s))    ### {'h', 'l', 'o', 'e'} <class 'set'>
  1. 集合元素是无序的,不可取出;转换成list可以取
s = {'a','b',1,2,3}
print(s,type(s))   ### {1, 2, 3, 'a', 'b'} <class 'set'>
## 不可取!无序的,不确定哪是第一个
# print(s[0])      ### TypeError: 'set' object is not subscriptable
print(list(s)[0])  ### 1
  1. .len():获取集合中元素的数量
### len():获取集合中元素的数量
s = {'a','b',1,2,3}
print(s,len(s))    ### {1, 2, 3, 'b', 'a'} 5
  1. .add()方法是向集合中添加元素
### add()方法是向集合中添加元素
s = {'a','b',1,2,3}
s.add(4)
s.add(5)
print(s)   ### {1, 2, 3, 4, 5, 'b', 'a'}
  1. .update(): 将()一个集合中的元素添加到另一个集合中
### update() 将()一个集合中的元素添加到另一个集合中
s = {'a','b',1,2,3}
s2 = set('hello')
s.update(s2)
print(s)    ### {1, 2, 3, 'b', 'l', 'h', 'o', 'e', 'a'}
  1. .pop() 随机删除集合中的一个元素
### .pop() 随机删除集合中的一个元素
s = {1, 2, 3, 'l', 'h', 'e', 'a', 'o', 'b', 'l'}
print("修改前:",s)       ### 修改前: {1, 2, 3, 'h', 'e', 'l', 'o', 'a', 'b'}
r = s.pop()
print("pop元素:",r)      ### 1
print("修改后:",s)       ### {2, 3, 'b', 'a', 'o', 'l', 'h', 'e'}
  1. .remove(元素),删除集合中指定的元素,无返回值
### .remove(元素),删除集合中指定的元素,无返回值
s = {1, 2, 3, 'l', 'h', 'e', 'a', 'o', 'b', 'l'}
print("修改前:",s)    ### 修改前:{1, 2, 3, 'h', 'e', 'l', 'o', 'a', 'b'}
s.remove(2)
print("修改后:",s)    ### 修改后:{1, 3, 'o', 'b', 'e', 'l', 'h', 'a'}
  1. .clear(),清空集合
### .clear(),清空集合
s = {1, 2, 3, 'l', 'h', 'e', 'a', 'o', 'b', 'l'}
print("修改前",s)    ### 修改前:{1, 2, 3, 'h', 'e', 'l', 'o', 'a', 'b'}
s.clear()
print("修改后",s)    ### 修改后:set()

7.3.4 集合的运算

  1. & 交集运算
### & 交集运算
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
r = s & s2
print(s,s2,r)  ### {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} {3, 4, 5}
  1. | 并集运算,去重
### | 并集运算,去重
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
r = s | s2
print(s,s2,r)  ### {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} {1, 2, 3, 4, 5, 6, 7}
  1. -差值运算
### - 取差集运算
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
r = s - s2
print(s,s2,r)    ### {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} {1, 2}
r = s2 - s
print(s,s2,r)    ### {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} {6, 7}
  1. ^ 亦或集,彼此不相交的部分
### ^ 亦或集,彼此不相交的部分
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
s3 = {6,7,8}
r = s2 ^ s ^ s3
print(s,s2,s3,r)   ### {1, 2, 3, 4, 5} {3, 4, 5, 6, 7} {8, 6, 7} {1, 2, 8}
  1. < 检查一个集合是否是另一个集合的真子集,返回布尔值
### < 检查一个集合是否是另一个集合的真子集,返回布尔值
a = {1,2,3}
b = {1,2,3,4,5}
r = a < b
print(r)    ### True

### 不是真子集
a = {1,2,3,4,5}
b = {1,2,3,4,5}
r = a < b
print(r)    ### False
  1. <= 检查一个集合是否是另一个集合的子集,返回布尔值
a = {1,2,3,4,5}
b = {1,2,3,4,5}
r2 = a <= b
print(r2)   ### True

### 真子集
a = {1,2,3}
b = {1,2,3,4,5}
r = a <= b
print(r)    ### True
  1. >= 检查一个集合是否是另一个集合的超集,返回布尔值
### >= 检查一个集合是否是另一个集合的超集
a = {1,2,3,4,5}
b = {1,2,3,4,5}
r3 = b >= a
print(r3)    ### True

### 真超集
a = {1,2,3,4,5}
b = {1,2,3,4,5,6,7}
r3 = b >= a
print(r3)    ### True
  1. > 检查一个集合是否是另一个集合的真超集,返回布尔值
## > 检查一个集合是否是另一个集合的真超集
a = {1,2,3}
b = {1,2,3,4,5}
r5 = b > a
print(r5)    ### True

### > 非真超集
a = {1,2,3,4,5}
b = {1,2,3,4,5}
r5 = b > a
print(r5)    ### False

7.4 小结

在这里插入图片描述

7.5 小练习

7.5.1 字典列表取值

  • 作业描述
    a = {“name”:“123”,“data”:{“result”:[{“src”:“python1”},{“src”:“python2”},{“src”:“python3”}]}} 找到python1/python2/python3
  • 实现参考
a = {"name":"123","data":{"result":[{"src":"python1"},{"src":"python2"},{"src":"python3"}]}}
print(a["data"]["result"],type(a["data"]["result"]))    ### [{'src': 'python1'}, {'src': 'python2'}, {'src': 'python3'}] <class 'list'>
print('Python1的表达方法: a["data"]["result"][0]["src"],即:',a["data"]["result"][0]["src"])
print('Python2的表达方法: a["data"]["result"][1]["src"],即:',a["data"]["result"][1]["src"])
print('Python3的表达方法: a["data"]["result"][2]["src"],即:',a["data"]["result"][2]["src"])

7.5.2 字典列表的保存

  • 作业描述
    有如下值集合[11,22,33,44,55,66,77,88,99,90], 将所有大于66的值保存至字典的第一个key中,将小于66值保存至第二个key的值中。
  • 实现参考
lst = [11,22,33,44,55,66,77,88,99,90]
dst = {"key1":[],"key2":[]}
for number in lst:
    if number > 66:
        dst["key1"].append(number)
    elif number < 66:
        dst["key2"].append(number)
print("dst:",dst)    ##{'key1': [77, 88, 99, 90], 'key2': [11, 22, 33, 44, 55]}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值