python - Python - 努力使用字典以以下格式打印
问题描述
我打印了一些看起来像这样的代码:
print(len(no_of_records))
200
150
2000
我有一个字典列表,例如:
list_names = {
"Yellow_List": "yellow",
"Blue_List": "blue",
"Red_List": "red"
}
我希望能够在字典中打印我的键名,记录数如下:
The yellow list has 200
The blue list has 150
The red list has 2000
我试过这个
for key in list_names:
print("The", key, "has", len(no_of_records))
但我得到:
The yellow list has 200
The blue list has 200
The red list has 200
The yellow list has 150
The blue list has 150
The red list has 150
The yellow list has 2000
The blue list has 2000
The red list has 2000
解决方案
你可以这样做:
no_records = [200, 150, 2000]
list_names = {
"Yellow_List": "yellow",
"Blue_List": "blue",
"Red_List": "red"
}
for key, x in zip(list_names.values(), no_records):
print('The', key, 'list has', x)
The yellow list has 200
The blue list has 150
The red list has 2000
推荐阅读
- javascript - 如何从灯箱画廊中触发无限滚动 - Django / FancyBox / Waypoints.js
- google-cloud-platform - 计算实例在 100% CPU 使用数小时后被删除
- c# - 使用 AT 命令发送包含表情符号的短信
- flutter - 如何更改颤动中图表的默认调色板?
- javascript - 如何使用 DRY 概念获取这些数据?
- saml-2.0 - 颁发者 ITfoxtec.Identity.Saml2 的多个证书
- express - 使用 vue cdn 分离组件文件
- python - Words 中的反斜杠冲突和 Pyparsing 中的换行符
- java - Gradle JAR 添加到类路径
- r - R 我可以将一个位置存储在某个东西中,这样我就可以多次引用它而不必重新输入它或者它是否会改变?