簡體   English   中英

將列表中的項目分配給 python 中另一個列表中的項目

[英]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.ptsself.fgm等,您應該使用 zip 遍歷most_statsf ,並使用zip中的相應子列表most_stats分配給f中的每個引用子列表:

for i, j in zip(f, most_stats):
    i[:] = j

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM