IndexError: list index out of range
Error message:
The code is as follows:
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = [splitFileNameAndLabel(filename = v) for v testFile.read().split(“\n”)]
The picture is as follows:
Error analysis:
The “IndexError: list index out of range” error typically occurs in two ways:
The first possible case:
List [index] Index is out of range
The second possibility:
The list is empty, with no elements
Listing [0] will report this error.
Typically, there is a problem with externally entered data, so it is usually processed once after readlines or read.
This avoids empty lines, insufficient fields, and type-casting errors.
Solutions:
For the first case, check the code through debugging, not much to say.
In the second case, there are two ways:
The first: check the data read in is there a problem, such as read in the TXT file whether there is a blank line and so on, but I more advocate the second solution, in the code error correction.
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = []
for v testFile.read().split(“\n”)
If not v.strip(): # string remove the space is not empty
continue
testfileList.extend([splitFileNameAndLabel(v)])
Note: [splitFileNameAndLabel(v)] is square again, otherwise it would not be a nested list in a list
The picture is as follows:
Error message:
The code is as follows:
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = [splitFileNameAndLabel(filename = v) for v testFile.read().split(“\n”)]
The picture is as follows:
Error analysis:
The “IndexError: list index out of range” error typically occurs in two ways:
The first possible case:
List [index] Index is out of range
The second possibility:
The list is empty, with no elements
Listing [0] will report this error.
Typically, there is a problem with externally entered data, so it is usually processed once after readlines or read.
This avoids empty lines, insufficient fields, and type-casting errors.
Solutions:
For the first case, check the code through debugging, not much to say.
In the second case, there are two ways:
The first: check the data read in is there a problem, such as read in the TXT file whether there is a blank line and so on, but I more advocate the second solution, in the code error correction.
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = []
for v testFile.read().split(“\n”)
If not v.strip(): # string remove the space is not empty
continue
testfileList.extend([splitFileNameAndLabel(v)])
Note: [splitFileNameAndLabel(v)] is square again, otherwise it would not be a nested list in a list
The picture is as follows:
Read More:
- panic: runtime error: index out of range
- String index out of range: 100 error report details and Solutions
- Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0
- Port out of range: – 1 for Tomcat startup in idea
- Python error type error: ‘range’ object does not support item assignment, solution
- Use of HQL query.list () is a null pointer exception, but the database can find out the result
- Pre initialization of list content and length in Python
- Index error: invalid index to scalar variable
- 206. Reverse Linked List [easy] (Python)
- Python common error: if using all scalar values, you must pass an index (four solutions)
- python-TypeError: list indices must be integers, not tuple Solution
- Python + selenium drop-down list error message: element not interactive
- On set in pandas_ Index and reset_ Usage of index
- [Solved] Es delete all the data in the index without deleting the index structure, including curl deletion
- python3 Error in sitecustomize; set PYTHONVERBOSE for traceback: NameError: name ‘modules_list’ …
- Runtimeerror using Python training model: CUDA out of memory error resolution
- Python installation problem: error: Command erred out with exit status 1:
- Python error: command error out with exit status 1:
- Error: command error out with exit status 1: Python setup.py egg_ info Check the ”
- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full comm