2017-03-18 68 views
0

目標是遍歷一個文件,並找到關於進攻籃板的前5名球隊和球員。在文件中包含多個團隊玩的遊戲。例如,灰熊vs熊隊將會出場,而來自灰熊隊的人可能會拿到3個籃板球,然後在灰熊隊和鯊魚隊之間排上幾百個籃板球,而某些人可能會拿到5個籃板球。它的格式如下:'Off Rebound(1)''Off Rebound(2)','Off Rebound(3)'等。奇怪的Python錯誤可能是由於循環造成的

目標是在遍歷整個文件的循環中放置一個循環。我能夠發現每場比賽籃板數最多的是10次。而不是做一些非常醜陋的事情,並且把10個if/else的陳述1-10,我想添加一些變量(k)1-10來減少它(我想抽出時間說我意識到代碼不是很好,我今晚就坐下來第一次看它,我對Python很新,我會當我得到全功能的代碼時,儘可能地修改它..也許這是一個不好的方法來做到這一點,但這就是我將要做的)。不幸的是,我得到了這個我在代碼下面粘貼的非常奇怪的錯誤。

import pandas as pd 
import collections as c 

fileRead = pd.read_csv('Sample.csv') 

eventDescript = fileRead.event_desc.apply(str) 
team = fileRead.team_name.apply(str) 
player = fileRead.player_name.apply(str) 
topTeams = [] 
topPlayers = [] 
i = 0 

while (i != len(eventDescript)): 
    # print eventDescript.where(eventDescript == 'Off Rebound (1)').dropna() 
    for k in range(1,11): 
     if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan': 
      topTeams.append(team[i]) 
      topPlayers.append(player[i]) 
      i = i + 1 
     else: 
      i = i + 1 
    i = i + 1 

print c.Counter(topTeams).most_common(5) 
print c.Counter(topPlayers).most_common(5) 

runfile('/Users/air13/Downloads/untitled2.py', wdir='/Users/air13/Downloads') 

錯誤: 回溯(最近通話最後一個):

File "<ipython-input-239-f1cd8a80a240>", line 1, in <module> 
    runfile('/Users/air13/Downloads/untitled2.py', wdir='/Users/air13/Downloads') 
    File "/Users/air13/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 
    File "/Users/air13/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile 
    builtins.execfile(filename, *where) 
    File "/Users/air13/Downloads/untitled2.py", line 24, in <module> 
    if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan': 
    File "/Users/air13/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 603, in __getitem__ 
    result = self.index.get_value(self, key) 
    File "/Users/air13/anaconda/lib/python2.7/site-packages/pandas/indexes/base.py", line 2169, in get_value 
    tz=getattr(series.dtype, 'tz', None)) 
    File "pandas/index.pyx", line 98, in pandas.index.IndexEngine.get_value (pandas/index.c:3557) 
    File "pandas/index.pyx", line 106, in pandas.index.IndexEngine.get_value (pandas/index.c:3240) 
    File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4279) 
    File "pandas/src/hashtable_class_helper.pxi", line 404, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8564) 
    File "pandas/src/hashtable_class_helper.pxi", line 410, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8508) 
KeyError: 128651 

回答

1

錯誤的線路上出現: if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan': 這是一個關鍵的錯誤,所以對於關鍵沒有價值的我在您的事件描述播放器字典。 這是因爲您使用的!=而不是在while循環<,並增加三次,讓我成爲比你的字典的長度。

下面是我會做:

import pandas as pd 
import collections as c 

fileRead = pd.read_csv('Sample.csv') 

eventDescript = fileRead.event_desc.apply(str) 
team = fileRead.team_name.apply(str) 
player = fileRead.player_name.apply(str) 
topTeams = [] 
topPlayers = [] 

for i in range(len(eventDescript)): 
    # print eventDescript.where(eventDescript == 'Off Rebound (1)').dropna() 
    if player[i] != 'nan': 
     k = int(eventDescript[i].replace('Off Rebound (', '').replace(')', '')) 
     if 1 <= k <= 10: 
      topTeams.append(team[i]) 
      topPlayers.append(player[i]) 

print(c.Counter(topTeams).most_common(5)) 
print(c.Counter(topPlayers).most_common(5)) 
  1. 我建議你總是使用for循環如果可能的話,那麼這樣的錯誤甚至不能發生。
  2. 你不需要第二個循環。
  3. 而不是i = i + 1你可以使用i + = 1
+0

我很感謝幫助,見過! – Tyler