python previous element in list loop
请帮忙解决这个问题。我需要将循环的元素与Python3中的前一个元素进行比较。下面的代码给出了错误:
TypeError: 'int' object is not subscriptable
1 2 3 | for index, (i, j) in enumerate(zip(a_list, b_list)): if j[index] == j[index-1]: a = 0 |
为什么不这么做?
1 2 3 4 5 | data = [1, 2, 2, 3, 4, 5, 5, 5, 3, 2, 7] for first, second in zip(data, data[1:]): if first == second: print('Got a match!') |
输出:
1 2 3 | Got a match! Got a match! Got a match! |