简体   繁体   English

为什么给我“列表索引超出范围”? 我正在尝试在列表中查找名称,并且名称的长度可以为两个或更多

[英]Why is this giving me “list index out of range”? I'm trying to find names in a list and names can be two words long or more

_ID = "000001234"
f = open("Players.txt", "r")
allLines = f.readlines()
nameList = [[] for i in range(1,len(allLines))]
for i in range(0,len(allLines)):
    splitLine = allLines[i].split()
    l = len(splitLine)
    for name in range(1,l-3):
        nameList[i].append(splitLine[name])
    #print(nameList[i])

for i in range(0, len(allLines)):
    splitLine = allLines[i].split()
    if splitLine[0] == _ID:
        print(nameList[i])
        break

f.close()   

Data: 数据:

Player ID                      Player Name          Age   Height(cm)   Weight(kg)
   000000001               Aaron Appindangoye           26      183         84.8
   000000002                  Aaron Cresswell           28      170         66.2
   000000003                      Aaron Doran           26      170         73.9
   000000004                    Aaron Galindo           36      183         89.8
   000000005                     Aaron Hughes           38      183         69.9
   000000006                       Aaron Hunt           31      183         73.0

Your nameList has a smaller length than a length of allLines : nameList比长度更小的长度allLines

>>> len(allLines)
>>> 7

however, 然而,

>>> len(nameList)
>>> 6

try nameList = [[] for i in range(0,len(allLines))] 尝试nameList = [[] for i in range(0,len(allLines))]

_ID = "000001234"
f = open("Players.txt", "r")
allLines = f.readlines()
nameList = [[] for i in range(0,len(allLines))]
for i in range(1,len(allLines)):
    splitLine = allLines[i].split()
    l = len(splitLine)
    for name in range(1,l-3):
        nameList[i].append(splitLine[name])
    #print(nameList[i])

for i in range(0, len(allLines)):
    splitLine = allLines[i].split()
    if splitLine[0] == _ID:
        print(nameList[i])
        break

f.close()  
nameList = [ii for ii in nameList[1:]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ IndexError:列表索引超出范围”试图使名称具有所有权 - “IndexError: list index out of range” trying to make names possessive 为什么csvkit给我“List Index out of Range”错误? - Why is csvkit giving me “List Index out of Range” errors? 为什么这会继续给我一个列表索引超出范围的错误? - Why does this keep on giving me a list index out of range error? 为什么它给我一个列表超出范围的错误? - Why is it giving me an list out of range error? 我可以使用什么来在两个列表中查找名称单词? Python - What can I use for finding names words in two list? Python 使用索引列表从另一个列表中删除会给我索引超出范围的错误-为什么? - Using a list of indexes to delete from a another list giving me index out of range error - why? 尝试 web 抓取并返回此错误:IndexError: list index out of range - Trying to web scrape and its giving me back this error: IndexError: list index out of range 谁能告诉我为什么我得到 IndexError: list index out of range? - Can anyone tell me why I get IndexError: list index out of range? 谁能帮我理解为什么我有错误“列表索引超出范围”? - Can anyone help me to understand why i have the error “list index out of range”? Python 尝试查找长度为 6 的列表的值 1 时列出“列表索引超出范围”错误 - Python Lists “list index out of range” error when trying to find value 1 of a list 6 long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM