当行未知时,如何在二维列表的For循环中定义范围?

2024-09-26 18:01:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在2d列表中有一个For循环,除非我希望循环一直持续到文件末尾,因为长度未知。你知道吗

这是我的密码:

while sales_search == 'Y':
    columns = 13
    with open('sales.txt', 'rt', encoding = 'utf8') as inf:
        data = list(csv.reader(inf,skipinitialspace=True))
        data = [i for i in data if i]
    rows = 0
    for row in range(rows):
        for col in range(columns):
            if (columns==0):
                found = False
                print ('****************')
                search = input ('What year do you want to search for:')
                if year == search:
                    print ('****************')
                    print ('Year:', (data[row][0]))
                    print ('The amount made from the Toyota Klugers is $',
                           format((data[row][1]),',.2f'))
                    print ('The amount made from the Nissan Patrols is $', 
                           format((data[row][2]),',.2f'))
                    print ('The amount made from the Ford Territorys is $', 
                           format((data[row][3]),',.2f'))
                    print ('The total amount made from all three cars is $', 
                           format((data[row][4]),',.2f'))                
                    print ('The bonus amount is $', 
                           format((data[row][5]),',.2f'))
                    print ('The bonus contributed through the sale of the Toyota Klugers is $', 
                           format((data[row][6]),',.2f'))
                    print ('The bonus contributed through the sale of the Nisssan Patrols is $', 
                           format((data[row][7]),',.2f'))
                    print ('The bonus contributed through the sale of the Ford Territorys is $', 
                           format((data[row][8]),',.2f'))
                    print ('The additional bonus for the Toyota Kluger is $', 
                           format((data[row][9]),',.2f'))
                    print ('The additional bonus for the Nissan Patrol is $', 
                           format((data[row][10]),',.2f'))
                    print ('The additional bonus for the Ford Territory is $', 
                           format((data[row][11]),',.2f'))
                    print ('The total bonus is $', 
                           format((data[row][12]),',.2f'))
                    found = True

如果能提供帮助,我将不胜感激:)


Tags: columnstheinfromformatforsearchdata
3条回答

现在你正在做for row in range(rows),也就是for row in range(0)。这实际上是在一个空列表上循环。你知道吗

>>> for row in range(0):
...     print row
... 
>>> 

相反,使用for row in data,它将把行分配给文件中的实际数据段:

while sales_search == 'Y':
            columns = 13
            with open('sales.txt', 'rt', encoding = 'utf8') as inf:
                data = list(csv.reader(inf,skipinitialspace=True))
                data = [i for i in data if i]
            rows = 0
            for row in data:
                for col in row:
                    #Do your stuff
                #Do your stuff

我想你想要

for row in data:

你可以在data上迭代。它是表示文件行的数组。你知道吗

with open('sales.txt', 'rt', encoding = 'utf8') as inf:
    data = list(csv.reader(inf,skipinitialspace=True))
    data = [i for i in data if i]
for row in data:
    for col in row:
        # do something with each column

    # ...or, as you were doing before...
    print ('Year:', row[0])
    print ('...', row[1])
    # etc.

相关问题 更多 >

    热门问题