Python list index start 1 question

Asked 2 years ago, Updated 2 years ago, 18 views

    f = open("text.txt", 'r',encoding='UTF8') 
    lines = f.readlines()
    Lastindex = (len(lines)-1) # Gets the last value index of the list
    for q in range(0,Lastindex+1):
     print(lines[q])
     self.tableWidget.setRowCount(q)
     self.tableWidget.setColumnCount(2)
     self.tableWidget.setItem(q-1, 0, QTableWidgetItem(lines[q]))

text.txt

First, the code is to load the contents of the text.txt file and display it in the tablewidget.

If you execute it, No. 1 on the memo pad is a PP file, but No. 1 starts with a transparent file as above

The result you want is I want to start from the 0th rank or the list index from the 1st place What should I do?

Thank you for your reply.

python

2022-09-20 11:04

1 Answers

Study the basics.

for i, v in enumerate(range(10, 20), start = 1):
    print(i, v)
>> 1 10
2 11
3 12
4 13
5 14
6 15
7 16
8 17
9 18
10 19
a = range(20, 30)

for i in a:
    b = a.index(i) + 1
    print(b, i)
>> 1 20
2 21
3 22
4 23
5 24
6 25
7 26
8 27
9 28
10 29


2022-09-20 11:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.