我尝试逐行读取文件并在读取时删除项目。我不知道我在这里做错了什么。在同一个日期有多个项目,所以我不得不用它们制作一个字典。
我的剧本:
'''
Read test data into simple dictionary for later use.
'''
matches_date = {}
with open('testi_data.csv','rb') as f:
for row in csv.reader(f, delimiter=';'):
if row[0] not in matches_date:
matches_date[row[0]] = []
matches_date[row[0]].append(row)
f.close()
'''
Read dictionary values sorted by date. Remove item when read.
'''
for item in sorted(matches_date.keys()):
for match in matches_date[item]:
if match[1] == 'blue':
score = [1,0]
else:
score = [0,1
date = match[0]
home = match[7]
away = match[8]
home_team = match[13]
away_team = match[14]
region = match[30]
print date, home, away, score
matches_date[date].remove(match)
输出:
20170510 G2 Esports SK Telecom T1 [0, 1]
20170510 GIGABYTE Marines Team SoloMid [1, 0]
20170510 SK Telecom T1 GIGABYTE Marines [1, 0]
当我希望它打印此文件中的每一行(testi_data.csv)时:
20170510;red;7.8;;;;;G2 Esports;SK Telecom T1;Kennen,Rumble,Karma,Fizz,Shen;LeBlanc,Jayce,Lulu,Fiora,Gangplank;Renekton,Lee Sin,Orianna,Ashe,Zyra;Galio,Graves,Syndra,Varus,Miss Fortune;Expect,Trick,Perkz,Zven,Mithy;Huni,Peanut,Faker,Bang,Wolf (Lee Jae-wan);43:53;71.8k;6;3;0;0;1;86.1k;10;11;5;2;0;2017 Mid-Season Invitational/Main Event/Scoreboards;http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH1/1002180106?gameHash=6000b59281dff531&tab=overview;International
20170510;blue;7.8;;;;;Team WE;Flash Wolves;Syndra,LeBlanc,Zyra,Shen,Gragas;Lulu,Karma,Graves,Tahm Kench,Nami;Kled,Elise,Taliyah,Ashe,Bard;Rumble,Lee Sin,Ryze,Varus,Malzahar;957,Condi,Xiye,Mystic,Ben;MMD,Karsa,Maple (Huang Yi-Tang),Betty,SwordArt;29:50;59.7k;16;9;1;1;1;48.1k;13;2;1;0;0;2017 Mid-Season Invitational/Main Event/Scoreboards;http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH1/1002180124?gameHash=a3c01e30df8a98a8;International
20170510;blue;7.8;;;;;GIGABYTE Marines;Team SoloMid;Ivern,Galio,Ashe,Caitlyn,Ezreal;Ziggs,Kennen,Syndra,Lee Sin,LeBlanc;Gragas,Kha'Zix,Ahri,Varus,Karma;Nautilus,Graves,Lulu,Twitch,Nami;Stark (Phan Công Minh),Levi,Optimus,Slay,Archie (Trần Minh Nhựt);Hauntzer,Svenskeren,Bjergsen,WildTurtle,Biofrost;36:11;69k;16;11;2;1;0;59.2k;15;4;2;0;0;2017 Mid-Season Invitational/Main Event/Scoreboards;http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH1/1002180130?gameHash=03336bc5758a759d;International
20170510;red;7.8;;;;;Flash Wolves;G2 Esports;Jayce,Kennen,Fiora,Renekton,Shen;Ashe,Graves,Lee Sin,Kled,Rumble;Gragas,Rengar,LeBlanc,Varus,Lulu;Nautilus,Ivern,Ryze,Caitlyn,Karma;MMD,Karsa,Maple (Huang Yi-Tang),Betty,SwordArt;Expect,Trick,Perkz,Zven,Mithy;46:27;81.6k;10;6;3;1;0;78.1k;8;8;1;0;0;2017 Mid-Season Invitational/Main Event/Scoreboards;http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH1/1002180138?gameHash=5604703d17a7f2a4;International
20170510;blue;7.8;;;;;SK Telecom T1;GIGABYTE Marines;LeBlanc,Gragas,Ziggs,Shen,Fizz;Galio,Kennen,Ezreal,Twitch,Kog'Maw;Rumble,Lee Sin,Ekko,Varus,Lulu;Jayce,Graves,Syndra,Ashe,Karma;Huni,Peanut,Faker,Bang,Wolf (Lee Jae-wan);Stark (Phan Công Minh),Levi,Optimus,Slay,Archie (Trần Minh Nhựt);26:45;58.3k;25;9;3;1;0;43.5k;12;0;0;0;0;2017 Mid-Season Invitational/Main Event/Scoreboards;http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH1/1002180145?gameHash=2ba736f5ffe8a59c&tab=overview;International
20170510;blue;7.8;;;;;Team SoloMid;Team WE;LeBlanc,Fizz,Lucian,Kled;Ivern,Varus,Syndra,Ryze,Orianna;Kennen,Lee Sin,Taliyah,Ezreal,Lulu;Jayce,Elise,Karma,Ashe,Nami;Hauntzer,Svenskeren,Bjergsen,WildTurtle,Biofrost;957,Condi,Xiye,Mystic,Ben;39:10;74.3k;10;11;2;2;0;63.5k;6;2;2;0;0;2017 Mid-Season Invitational/Main Event/Scoreboards;http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH1/1002180154?gameHash=e1b11e4d1901c49b;International
我需要能够删除这些读取的项目,以便进一步使用字典。
答案 0 :(得分:1)
迭代列表时不要删除项目。它会导致你不会期望的行为。有answer解释原因。由于您的代码将删除所有元素并留下matches_date[item]
的空列表,因此您可以简单地将matches_date[item] = []
放在内部循环之外。
答案 1 :(得分:1)
回答是因为我想粘贴一个代码示例。
正如@gzc所提到的,不要从for循环中迭代的列表中删除。当你删除一个项目时,整个列表会重新排列到左边,而索引会继续前进(或更准确地说,在循环结束之前一直保持在同一位置)。对于您删除的每个项目,您的索引会跳过您可能想要浏览的另一个项目。
lst = ["abc", "abc2", "abc3", "abc4", "abc5"]
for i, var in enumerate(lst):
print("index: " + str(i) + " var: " + var)
lst.remove(lst[i])
print(lst)
对于此代码,结果为:
index: 0 var: abc
index: 1 var: abc3
index: 2 var: abc5
['abc2', 'abc4']
删除项目时,必须向后移动索引,而不能在for循环中完成。最好使用while循环,这样可以在删除项目时将索引移回。