[英]assigning items in a list to items in another list in python
我有兩個清單。
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]
所有屬性都是空列表。
我想編寫一個循環,將每個屬性分配給大多數統計信息中的索引。 每個索引都匹配其屬性的索引 position,意思是:
f[0] = most_stats[0]
f[1] = most_stats[1]
f[2] = most_stats[2] ...
通過做:
f[0] = most_stats[0]
您正在覆蓋索引0
處的f
值,而不是使用most_stats[0]
更新self.pts
引用的列表。
因此,如果目標是更新self.pts
、 self.fgm
等,您應該使用 zip 遍歷most_stats
和f
,並使用zip
中的相應子列表most_stats
分配給f
中的每個引用子列表:
for i, j in zip(f, most_stats):
i[:] = j
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.