python - 如何在同一个 for 语句中迭代多个不同的值?
问题描述
错误返回 ValueError: too many values to unpack (expected 2)
accounts = [["Tom", "Boyle", "23"]]
for meta in "First Name", "Last Name", "Age":
for account in accounts:
# The line below is what I have trouble with
for info, meta in account, ("First Name", "Last Name", "Age"):
print(meta + ": " + info)
print("------------------------------------------")
""" Expected Output:
First Name: Tom
Last Lame: Boyle
Age: 23
------------------------------------------
"""
解决方案
我认为您正在寻找的是:
accounts = [["Tom", "Boyle", "23"]]
meta_list = ["First Name", "Last Name", "Age"]
for account in accounts:
for meta, info in zip(meta_list,account):
print(meta + ": " + info)
print("------------------------------------------")
推荐阅读
- python - 逐类元素返回无
- r - 如何在基本 R plot() 中绘制正交向量?
- c# - 忽略单引号内的嵌套单引号
- excel - AttributeError:“元组”对象没有属性“值”PYTHON TO EXCEL
- wordpress - 当用户在 WooCommerce 中免税时,如何以编程方式获取税额?
- office-js - ScriptLab 数据透视表示例不再起作用
- jquery - 基于类别选择栏呈现字段
- vue.js - Vuetify 日期选择器 - 如何设置对话框使用的初始日期
- python - 在 OSmnx 中将街道划分为更小的部分
- firebase - Firebase 存储安全规则 - 对象的属性大小未定义