迭代列表列表,跳过第一个列表

时间:2021-04-21 22:34:41

The first list within my list of lists looks something like this

列表列表中的第一个列表看起来像这样

[['QGC', 'WPL', '110'], ['0', '1', '0', '16', '0', '0', '0', '0', '35.650418', '-78.313229', '100.000000', '1'], ['1', '0', ........

I want to skip the first list within my for loop, and compare the first element of the next list with another list. I have this code so far but don't think this is correct or efficient because I keep getting the following error

我想跳过for循环中的第一个列表,并将下一个列表的第一个元素与另一个列表进行比较。到目前为止我有这个代码,但不认为这是正确或有效的,因为我不断收到以下错误

 print(lines[0])
IndexError: list index out of range

The code:

for lines in allrecords[1:len(allrecords):1]:
    for i, num in enumerate(linesOfFileToChange):
        #print(num)
        print(lines[0])

3 个解决方案

#1


0  

What about:

for lines in allrecords:
    for i, num in enumerate(lines[:1]):
        # do something

#2


0  

You can do it this way -

你可以这样做 -

for index, values in enumerate(allrecords):
    if index == 0:
       continue
    value = values[0] if values else None
    print value

OR

new_records = allrecords[1:]
for values in new_records:
    print next(iter(values), None)

#3


0  

From what I understood you want to compare lists with numbers in allrecords. So you want to compare first list with second, then second with third, third with fourth and so on.

根据我的理解,您希望将列表与所有记录中的数字进行比较。因此,您希望将第一个列表与第二个列表进行比较,然后将第二个列表与第三个列表进

allrecords = [
    ['QGC', 'WPL', '110'],
    ['1', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
    ['2', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
    ['3', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
    ['4', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
]

for idx_lst, lst in enumerate(allrecords[1:-1]):
    next_lst = allrecords[idx_lst+2]
    for lst_value, next_lst_value in zip(lst, next_lst):
        # do some operations here
        print lst_value, next_lst_value, "|" ,
    print ""

Output:

1 2 | 1 1 | 0 0 | 16 16 | 0 0 | 0 0 | 0 0 | 0 0 | 35.650418 35.650418 |
2 3 | 1 1 | 0 0 | 16 16 | 0 0 | 0 0 | 0 0 | 0 0 | 35.650418 35.650418 |
3 4 | 1 1 | 0 0 | 16 16 | 0 0 | 0 0 | 0 0 | 0 0 | 35.650418 35.650418 |

So instead of printing pairs of values you can do some operations there.

因此,您可以在那里进行一些操作,而不是打印成对的值。

#1


0  

What about:

for lines in allrecords:
    for i, num in enumerate(lines[:1]):
        # do something

#2


0  

You can do it this way -

你可以这样做 -

for index, values in enumerate(allrecords):
    if index == 0:
       continue
    value = values[0] if values else None
    print value

OR

new_records = allrecords[1:]
for values in new_records:
    print next(iter(values), None)

#3


0  

From what I understood you want to compare lists with numbers in allrecords. So you want to compare first list with second, then second with third, third with fourth and so on.

根据我的理解,您希望将列表与所有记录中的数字进行比较。因此,您希望将第一个列表与第二个列表进行比较,然后将第二个列表与第三个列表进

allrecords = [
    ['QGC', 'WPL', '110'],
    ['1', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
    ['2', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
    ['3', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
    ['4', '1', '0', '16', '0', '0', '0', '0', '35.650418'],
]

for idx_lst, lst in enumerate(allrecords[1:-1]):
    next_lst = allrecords[idx_lst+2]
    for lst_value, next_lst_value in zip(lst, next_lst):
        # do some operations here
        print lst_value, next_lst_value, "|" ,
    print ""

Output:

1 2 | 1 1 | 0 0 | 16 16 | 0 0 | 0 0 | 0 0 | 0 0 | 35.650418 35.650418 |
2 3 | 1 1 | 0 0 | 16 16 | 0 0 | 0 0 | 0 0 | 0 0 | 35.650418 35.650418 |
3 4 | 1 1 | 0 0 | 16 16 | 0 0 | 0 0 | 0 0 | 0 0 | 35.650418 35.650418 |

So instead of printing pairs of values you can do some operations there.

因此,您可以在那里进行一些操作,而不是打印成对的值。