2017-06-16 37 views
-3

我在Python中比較新,我試圖使用beautifulSoup來刮一些棒球數據,這需要我知道玩家ID,他們玩的位置,以及我想知道他們蝙蝠從哪一方決定我需要搶下哪些擊球。這是我使用一些測試數據的全部程序代碼中的一小部分,因爲我一次測試小部分。循環遍歷列表,同時也使用if語句使用Python不工作

我需要循環數百名玩家。但是,它似乎沒有在我的列表中循環越過[1] index。我不知道爲什麼這不起作用,因爲我通常知道如何使用for循環。

此外,while i = 1,它給我的˚F的位置這將是[1]字符,但我想在列表中[1]項目(這將是P)。

的代碼如下:

battingSide = ['R','L','S','R','S','R','R','L'] 
position = ['OF','P','1B','2B','SS','3B','P','P','P','P','P','P','P'] 
playerID = [123,4,5,6,7,8,9,11] 


i = 0 
while i < len(battingSide): 
    position = position[i] 
    activePlayerID = playerID[i] 
    if battingSide[i] == 'R': 
     print(i) 
     splits = [0.3,0.4,0.5] 
     print(splits) 
     print(position) 
     print(activePlayerID) 
      i +=1 
    elif battingSide[i] == 'L': 
     splits = [0.1,0.2] 
     print(splits) 
     print(position) 
     print(activePlayerID) 
     print(playerID) 
     i+=1 
    else: 
    # print(i) 
     splits = [0.1,0.2,0.3,0.4] 
     print(splits) 
     print(position) 
     print(activePlayerID) 
     print(playerID) 
     i+=1 --(I'm not sure if I need this but I added it to try and fix the issue - however it didn't work) 

的錯誤如下:

Traceback (most recent call last): File "C:/Users/Schreier Family/Desktop/Python Programs/playerID - position - bats.py", line 8, in position = position[i] IndexError: string index out of range

我得到的錯誤如下所示之前的輸出:

0 
[0.3, 0.4, 0.5] 
F 
123 
[0.1, 0.2] 
F 
4 
[123, 4, 5, 6, 7, 8, 9, 11] 

隨着我繼續學習Python,任何幫助都將不勝感激!

回答

0

您shouldn't使用相同的名稱爲變量位置和排列位置:

position = position[i] 

這條線是不正確的,因爲在第一次迭代你失去的陣列,則必須將其更改爲samething像這樣:

actual_position = position[i] 

,然後你needo改變所有出場

print(position) 

print(actual_position) 

所以,你會有一些其他的變化之後,以優化:

battingSide = ['R','L','S','R','S','R','R','L'] 
position = ['OF','P','1B','2B','SS','3B','P','P','P','P','P','P','P'] 
playerID = [123,4,5,6,7,8,9,11] 

i = 0 
while i < len(battingSide): 
    actua_position = position[i] 
    activePlayerID = playerID[i] 
    print(i) 
    if battingSide[i] == 'R': 
     splits = [0.3,0.4,0.5] 
    elif battingSide[i] == 'L': 
     splits = [0.1,0.2] 
    else: 
     splits = [0.1,0.2,0.3,0.4] 
    print(splits) 
    print(actual_position) 
    print(activePlayerID) 
    print(playerID) 
    i+=1 
+0

我喜歡這個,因爲它簡化了的東西 - 但是,我不需要在年底全部打印 - 這更多的是一個測試功能,以確保如果它是工作與否。謝謝你的幫助! –

+0

我不能相信我錯過了,因爲我爲此更改了playerID到activePlayerID。再次感謝您的幫助! –

0

看來你不小心被包含第一元素的變量替換position您的列表position。下面是修改的版本:

i = 0 
while i < len(battingSide): 
    pos = position[i] 
    activePlayerID = playerID[i] 
    if battingSide[i] == 'R': 
     print(i) 
     splits = [0.3,0.4,0.5] 
     print(splits) 
     print(pos) 
     print(activePlayerID) 
     i +=1 
    elif battingSide[i] == 'L': 
     splits = [0.1,0.2] 
     print(splits) 
     print(pos) 
     print(activePlayerID) 
     print(playerID) 
     i+=1 
    else: 
    # print(i) 
     splits = [0.1,0.2,0.3,0.4] 
     print(splits) 
     print(pos) 
     print(activePlayerID) 
     print(playerID) 
     i+=1 
0
battingSide =['R','L','S','R','S','R','R','L'] 
position =['OF','P','1B','2B','SS','3B','P','P','P','P','P','P','P'] 
playerID =[123,4,5,6,7,8,9,11] 
i=0 
position1=[]*len(position) 
while i < len(battingSide): 
    position1 = position[i] 
    activePlayerID = playerID[i] 
    if battingSide[i] == 'R': 
     print(i) 
     splits = [0.3,0.4,0.5] 
     print(splits) 
     print(position1) 
     print(activePlayerID) 
     i +=1 
    elif battingSide[i] == 'L': 
     splits = [0.1,0.2] 
     print(splits) 
     print(position) 
     print(activePlayerID) 
     print(playerID) 
     i+=1 
    else: 
    # print(i) 
     splits = [0.1,0.2,0.3,0.4] 
     print(splits) 
     print(position1) 
     print(activePlayerID) 
     print(playerID) 
     i+=1 
+0

你可以在你改變的內容上添加一些解釋嗎? – Colwin

+0

位置正在重新初始化,所以我更改了名稱並初始化了一個Len列表(位置) –

+0

請編輯您的答案以添加您的解釋。 – Colwin