0

I'm trying to read a file line by line and remove items when read. I don't know what I'm doing wrong here. There are multiple items at same date so I had to make a dictionary out of them.

My script:

'''
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)

Output:

20170510 G2 Esports SK Telecom T1 [0, 1]

20170510 GIGABYTE Marines Team SoloMid [1, 0]

20170510 SK Telecom T1 GIGABYTE Marines [1, 0]

When I want it to print every line from this file (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

I need to be able to remove those read items for further use of the dictionary.

martineau
  • 99,260
  • 22
  • 139
  • 249
tmi12
  • 47
  • 1
  • 7

2 Answers2

1

Don't remove items while iterating list. It will cause behavior you wouldn't expect. There is an answer explain why. Since your code will remove all elements and leave empty list for matches_date[item], You can simply put matches_date[item] = [] outside inner loop.

gzc
  • 6,154
  • 5
  • 32
  • 59
  • I dont want to make the entire list empty. Just remove the item that has been read. – tmi12 May 28 '17 at 17:10
  • @tmi12 Your inner loop is iterating over whole `matches_date[item]` and removing each element. – gzc May 28 '17 at 17:13
1

Making an answer because I want to paste a code example.

As @gzc mentioned, do not remove from a list you are iterating over when in a for loop. When you remove an item the entire list rearranges to the left while the index keeps going forward (or to be more exact, stays at the same spot until the end of the cycle). With every item you remove your index skips another item that you probably wanted to go through.

lst = ["abc", "abc2", "abc3", "abc4", "abc5"]

for i, var in enumerate(lst):
    print("index: " + str(i) + " var: " + var)
    lst.remove(lst[i])
print(lst)

For this code the result is:

index: 0 var: abc
index: 1 var: abc3
index: 2 var: abc5
['abc2', 'abc4']

While you remove items, you must move the index backwards and it can't be done in a for loop. You are better off with a while loop that will enable you to move the index back when an item is removed.

BoobyTrap
  • 890
  • 7
  • 18