循环遍历列表,同时也使用if语句使用Python不工作

时间:2017-06-16 03:02:53

标签: python loops for-loop if-statement

我在Python方面比较陌生,我正在尝试使用 beautifulSoup 来抓取一些棒球数据,这需要我知道玩家ID,他们玩的位置,我想知道哪一方他们击球以确定我需要抓住哪些击球分组。这是我的整个程序代码的一小部分,使用一些测试数据,因为我一次测试一小部分。

我需要循环数百名玩家。但是,它似乎没有循环遍历我的列表中的[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)

错误如下:

  

回溯(最近一次调用最后一次):文件&#34; C:/ Users / Schreier系列/桌面/ Python程序/ playerID - 位置 - bats.py&#34;,第8行,位置=位置[i] IndexError:字符串索引超出范围

我在错误发生之前得到的输出如下:

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

我继续学习Python,非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

您不应对变量位置和数组位置使用相同的名称:

position = position[i]

该行不正确,因为在第一次迭代中你松开了数组,你必须把它改成这样的东西:

actual_position = position[i]

然后你需要改变

的所有外观
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

答案 1 :(得分: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

答案 2 :(得分: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