python - 如何在 Python 中修复 ValueError : not enough values to unpack (expected 3, got 1)?

标签 python iterable-unpacking

我有一个名为 records 的数据集,数据集示例如下所示:

first_record = records[0]
print(first_record)
_____________________________
['1', '1001', 'Movie']

我想提取每个值以进行进一步计算,当我执行以下代码时:

for user, item, tag in first_record :
    print(user, item, tag)

我有这个错误:

----> 1 for user, item, tag in first_record :
      2     print(user, item, tag)

ValueError: not enough values to unpack (expected 3, got 1)

如何提取数据集中与我的用户、商品、标签变量对应的每个值?

最佳答案

看起来您打算遍历 records,而不是 first_record(records 列表中的第一个元素),并且对于每条记录打印这三个值:

for user, item, tag in records:
    print(user, item, tag)

关于python - 如何在 Python 中修复 ValueError : not enough values to unpack (expected 3, got 1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62965928/

相关文章:

python - 将python中的字符串从左边开始分成两部分

python - 使用 python 2.7.13 请求的 ssl 连接

python - 解压 1 个变量,剩下一个列表

python - 如何在 Python 2 中正确编码向量化函数条目

python - 为什么函数参数中的元组赋值在 python3 中不起作用

python - 具有两个分类变量的数据帧上的 Groupby 和 count()

python - 如何在 PySpark 中分别对多个列进行透视

python - 元组如何在 for 循环中解包?

python - 在 Python 中解压给定数量的项目?

python - 如何在 scipy 中读取稀疏有向图