简体   繁体   English

如何遍历python数组并将元素与前一个元素进行比较?

[英]How to iterate through a python array and compare the element to the previous element?

I'm having a bit of trouble with an array in python. 我在python中遇到一个数组有点麻烦。 I want to loop through it, and compare element n to element n-1. 我想循环遍历它,并将元素n与元素n-1进行比较。 For example: 例如:

 [(11, 11), (11, 10), (11, 9), (11, 8), (11, 7), (11, 6), (11, 5),
  (11, 4), (10, 4), (9, 4), (8, 4), (8, 5), (7, 5), (6, 5), (5, 5),
  (4, 5), (3, 5), (3, 4), (3, 3), (2, 3), (1, 3), (1, 2), (1, 1), (1, 0)]

Using the above array, I want to apply the following moves/logic: 使用上面的数组,我想应用以下移动/逻辑:

  • 0,1 = right 0,1 =对

  • 1,0 = down 1,0 =下降

  • -1,0 = up -1,0 =向上

  • 0,-1 = left 0,-1 =左

So if the element of the array we are looking at's first value is less than the previous I want to print up. 因此,如果我们看到的第一个值的数组元素小于我想要打印的前一个值。

So the result for the array above would be (assuming the start is always 0,0) 所以上面数组的结果是(假设开始总是0,0)

[Start, down, right, right, right, down, down, right, right, down, 
down, down, down, down, left, down, down, down, right, right, right, 
right, right, right, right] 

It's a tricky one to explain so apologies if this is a bit confusing. 如果这有点令人困惑,那么解释如此道歉是一个棘手的问题。 Also the element will never go diagonal so it will never got from (1,1) to (2,2) one one of the 2 sub-elements will change at any given time. 此外,元素将永远不会对角线,因此它永远不会从(1,1)到(2,2),在任何给定时间,2个子元素中的一个将发生变化。

Considering that your array of coordinates is called coords , you could do: 考虑到您的坐标数组称为coords ,您可以这样做:

steps = [(x2-x1, y2-y1) for ((x1, y1), (x2, y2)) in  zip(coords, coords[1:])]

Explanation: 说明:

  • coords[1:] represents all coordinate pairs starting with the second one coords [1:]表示从第二个开始的所有坐标对
  • zip(coords, coords[1:]) makes pairs of each coordinate with the one before zip(coords,coords [1:])使每个坐标与之前的坐标成对
  • for an array of n corrdinate pairs, zip will output n-1 steps, since zip 's output length is equal to the length of the shorter one of its arguments. 对于n corrdinate对的数组, zip将输出n-1步,因为zip的输出长度等于其较短的一个参数的长度。 So, due to the second list being shorter, the first list will only be enumerated excluding its last element (thanks Ev. Kounis for suggesting this clarification) 因此,由于第二个列表较短,第一个列表将仅被枚举,不包括其最后一个元素(感谢Ev.Kounis建议澄清)

EDIT: to make a list of strings representing the steps made, you can make a dictionary of possible movements: 编辑:要创建表示所做步骤的字符串列表,您可以制作可能的移动字典:

coords = [(11, 11), (11, 10), (11, 9), (11, 8), (11, 7), (11, 6),
          (11, 5), (11, 4), (10, 4), (9, 4), (8, 4), (8, 5), (7, 5),
          (6, 5), (5, 5), (4, 5), (3, 5), (3, 4), (3, 3), (2, 3),
          (1, 3), (1, 2), (1, 1), (1, 0)]

movements = {
        (0, 1):  'right',
        (1, 0):  'down',
        (-1, 0): 'up',
        (0, -1): 'left'
        }

steps = [movements[(x2-x1, y2-y1)]
         for ((x1, y1), (x2, y2)) in zip(coords, coords[1:])]

You could always just access index i-1 for the previous element starting at i=1 : 你总是可以从i=1开始访问前一个元素的索引i-1 i=1

from operator import sub

lst = [(11, 11), (11, 10), (11, 9), (11, 8), (11, 7), (11, 6), (11, 5), (11, 4), (10, 4), (9, 4), (8, 4), (8, 5), (7, 5), (6, 5), (5, 5), (4, 5), (3, 5), (3, 4), (3, 3), (2, 3), (1, 3), (1, 2), (1, 1), (1, 0)]

d = {(0, 1):'right', (1, 0): 'down', (-1, 0): 'up', (0, -1): 'left'}

result = [d[tuple(map(sub, lst[i], lst[i-1]))] for i in range(1, len(lst))]

print(result)
# ['left', 'left', 'left', 'left', 'left', 'left', 'left', 'up', 'up', 'up', 'right', 'up', 'up', 'up', 'up', 'up', 'left', 'left', 'up', 'up', 'left', 'left', 'left']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM