简体   繁体   English

在二维数组中查找点

[英]Finding points in a 2d array

I've started coding recently and my preferred language is python. 我最近开始编码,我的首选语言是python。 I've come across a problem I'm having trouble with. 我遇到了遇到麻烦的问题。

The problem is finding the index in a 2d array where the number is either larger than the numbers to the left and right and smaller than the numbers above and below or vice versa. 问题是在2d数组中找到索引,该数组中的数字要么大于左右数字,要么小于上下数字,反之亦然。

I know a 2d array can be defined as list = [[1, 2], [3, 4], [5, 6]] but as for working out the algorithm to sort the problem is at the present time beyond me. 我知道可以将2d数组定义为list = [[1, 2], [3, 4], [5, 6]]但是目前尚无法确定解决该问题的算法。 Could someone please offer a solution? 有人可以提供解决方案吗?

min(enumerate(list[0]),key=lambda x:x[1]) will find the (index,value) pair with the smallest value from list[0] ((0,1) in this example). min(enumerate(list[0]),key=lambda x:x[1])将从list [0](在此示例中为(0,1) min(enumerate(list[0]),key=lambda x:x[1])找到具有最小值的(索引,值)对。 max(enumerate(list[0]),key=lambda x:x[1]) will find the largest. max(enumerate(list[0]),key=lambda x:x[1])将找到最大的。

Note (since you said you're new) this is the same as: 注意(因为您说的是新手),这与以下内容相同:

def first_index(L):
    return l[1]
min(enumerate(list[0]),key=first_index)

new_list=[L[0] for L in list] will make a list containing the 0th element from each list ([1,3,5] in this example). new_list=[L[0] for L in list]将创建一个包含每个列表中第0个元素的列表(在此示例中为[1,3,5])。 You'll probably want to do this for each column using for index in range(len(list[0]): 您可能希望使用for index in range(len(list[0]):为每一列执行此操作for index in range(len(list[0]):

Note (since you're new) this is the same as: 注意(因为您是新手)因此与以下内容相同:

new_list=list()
for L in list:
    new_list.append(L[0])

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

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