我有一个名为 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/