python 如何读取列表中字典的value值
作者:katios 时间:2021-01-27 15:48:31
读取列表中字典的value值
list = [{"name": "推荐食谱", "1": "症状", "name1": "浑身忽冷忽热"}, {"name": "绿豆薏米饭"}, {"name": "芝麻"}]
res = [item[key] for item in list for key in item]
print(res)
for item in list:
for key in item:
print(item[key])
list = [{"name": "推荐食谱", "1": "症状", "name1": "浑身忽冷忽热"}, {"name": "绿豆薏米饭"}, {"name": "芝麻"}]
res = [item[key] for item in list for key in item]
print(res)
for item in list:
for k,v in item.items():
print(k,v)
获取字典的key和value
以迭代的方式,默认情况下,字典迭代的是key,key相当于item里面的[0]位置,value是[1]位置
a.items()
key=a.items[0]
value=a.items[1]
for item in dict.items():
key = item[0]
value = item[1]
打印每一个key
a = {'a':1,'b':2,'c':3}
for item in a.items():
print(item[0])
>>>
a
b
c
打印每一个value
a = {'a':1,'b':2,'c':3}
for item in a.items():
print(item[1])
>>>
1
2
3
不通过dict.item()这个函数,直接利用默认迭代是key这个特性,可以直接迭代
>>> dict = {'a': 1, 'b': 2, 'c': 3}
>>> for key in dict:
... print(key)
...
a
c
b
第二种方式,如果要迭代value,可以用a.values(),这个是找值,找键是a.keys()
a = {'a':1,'b':2,'c':3}
for value in a.values():
print(value)
>>>
1
2
3
for key in a.keys():
print(key)
>>>
a
b
c
第三种,如果要同时迭代key和value,可以用for k, v in d.items()。
a = {'a':1,'b':2,'c':3}
for k,v in a.items():
print(k)
print(v)
>>>
a
1
b
2
c
3
输出全部的key和value,但不是list格式
a = {'a':1,'b':2,'c':3}
print(a.values())
>>> dict_values([1, 2, 3])
print(a.keys())
>>>dict_keys(['a', 'b', 'c'])
如果要变成list列表,转换类型即可
list(a.keys())
>>>['a', 'b', 'c']
来源:https://blog.csdn.net/liuskyter/article/details/91868708
标签:python,列表,字典,value
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python制作运行进度条的实现效果(代码运行不无聊)
2021-04-11 15:46:10
![](https://img.aspxhome.com/file/2023/6/105196_0s.gif)
python pyinstaller打包exe报错的解决方法
2022-05-11 20:39:40
![](https://img.aspxhome.com/file/2023/2/117412_0s.png)
10条改进你的CSS代码的方法
2010-03-20 22:07:00
![](https://img.aspxhome.com/file/UploadPic/20103/20/best-css-02-10s.jpg)
SQL Server的FileStream和FileTable深入剖析
2023-07-17 01:17:24
![](https://img.aspxhome.com/file/UploadPic/201211/30/20121130203621460s.jpg)
python实现知乎高颜值图片爬取
2023-03-11 10:35:54
使用python调用zxing库生成二维码图片详解
2022-03-19 06:41:09
![](https://img.aspxhome.com/file/2023/7/90787_0s.png)
简述python四种分词工具,盘点哪个更好用?
2021-08-22 23:28:01
Go语言中的Array、Slice、Map和Set使用详解
2023-06-24 07:29:03
Python中使用matplotlib模块errorbar函数绘制误差棒图实例代码
2022-11-09 17:01:42
![](https://img.aspxhome.com/file/2023/5/79435_0s.png)
Python实现按中文排序的方法示例
2023-11-29 15:19:22
![](https://img.aspxhome.com/file/2023/2/92142_0s.png)
Python3安装pip工具的详细步骤
2021-09-27 15:38:09
![](https://img.aspxhome.com/file/2023/3/72923_0s.png)
asp中常用的文件处理函数
2009-01-08 18:09:00
python的numpy模块安装不成功简单解决方法总结
2022-10-23 18:33:15
![](https://img.aspxhome.com/file/2023/6/66396_0s.png)
Python+PyQt5制作一个图片查看器
2021-03-03 04:16:06
![](https://img.aspxhome.com/file/2023/5/87995_0s.gif)
PHP图像处理技术实例总结【绘图、水印、验证码、图像压缩】
2023-10-03 00:48:07
![](https://img.aspxhome.com/file/2023/9/55489_0s.png)
Python进程间通讯与进程池超详细讲解
2023-09-05 16:50:41
python字典遍历数据的具体做法
2022-04-19 16:45:33
Django 解决distinct无法去除重复数据的问题
2023-02-09 22:13:22
基于Keras的格式化输出Loss实现方式
2021-10-20 20:44:00
![](https://img.aspxhome.com/file/2023/2/92312_0s.jpg)
Python采集情感音频的实现示例
2023-06-11 23:17:10
![](https://img.aspxhome.com/file/2023/6/75076_0s.png)