我有两个列表。
for row in self.json_data['athletes']:
most_stats = row['categories'][1]['totals'][0:11]
f = [self.pts, self.fgm, self.fga, self.fgp, self.three_pm, self.three_attempted,
self.three_percent,
self.ftm, self.fta, self.percent_ft, self.ast]
所有属性都是空列表。
我想编写一个循环,将大多数统计数据中的每个属性分配给索引。 每个索引都与其属性的索引位置匹配,含义:
f[0] = most_stats[0]
f[1] = most_stats[1]
f[2] = most_stats[2] ...
最佳答案
通过这样做:
f[0] = most_stats[0]
您正在覆盖 f
的值在索引0
,而不是更新 self.pts
引用的列表与 most_stats[0]
.
如果目标是更新self.pts
, self.fgm
依此类推,您应该迭代 most_stats
与 f
使用zip
并分配给 f
中的每个引用子列表与 most_stats
中相应的子列表就地:
for i, j in zip(f, most_stats):
i[:] = j
关于python - 在python中将列表中的项目分配给另一个列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58999801/