简体   繁体   English

使用 python 在二维列表中搜索以查找 x,y 位置

[英]Search in 2D list using python to find x,y position

I have 2D list and I need to search for the index of an element.我有二维列表,我需要搜索元素的索引。 As I am begineer to programming I used the following function:当我开始编程时,我使用了以下功能:

def in_list(c):
    for i in xrange(0,no_classes):
        if c in classes[i]:
            return i;

    return -1

Here classes is a 2D list and no_classes denotes the number of classes ie the 1st dimesntion of the list.这里的类是一个二维列表,no_classes 表示类的数量,即列表的第一个维度。 -1 is returned when c is not in the araray.当 c 不在数组中时返回 -1。 Is there any I can optimize the search?有什么我可以优化搜索的吗?

You don't need to define no_classes yourself.您不需要自己定义no_classes Use enumerate() :使用enumerate()

def in_list(c, classes):
    for i, sublist in enumerate(classes):
        if c in sublist:
            return i
    return -1

Use list.index(item)使用 list.index(item)

a = [[1,2],[3,4,5]]

def in_list(item,L):
    for i in L:
        if item in i:
            return L.index(i)
    return -1

print in_list(3,a)
# prints 1

if order doesn't matter and you have no duplicates in your data, I suggest to turn you 2D list into list of sets:如果顺序无关紧要并且您的数据中没有重复项,我建议您将 2D 列表转换为集合列表:

>>> l = [[1, 2, 4], [6, 7, 8], [9, 5, 10]]
>>> l = [set(x) for x in l]
>>> l
[set([1, 2, 4]), set([8, 6, 7]), set([9, 10, 5])]

After that, your original function will work faster, because search of element in set is constant (while search of element in list is linear), so you algorithm becomes O(N) and not O(N^2).之后,您的原始函数将更快地工作,因为在集合中的元素搜索是恒定的(而在列表中的元素搜索是线性的),所以您的算法变为 O(N) 而不是 O(N^2)。

Note that you should not do this in your function or it would be converted each time function is called.请注意,您不应在函数中执行此操作,否则每次调用函数时都会对其进行转换。

If your "2D" list is rectangular (same number of columns for each line), you should convert it to a numpy.ndarray and use numpy functionalities to do the search.如果您的“2D”列表是矩形的(每行的列数相同),您应该将其转换为numpy.ndarray并使用 numpy 功能进行搜索。 For an array of integers, you can use == for comparison.对于整数数组,可以使用==进行比较。 For an array of float numbers, you should use np.isclose instead:对于浮点数数组,您应该使用np.isclose代替:

a = np.array(c, dtype=int)
i,j = np.where(a == element)

or或者

a = np.array(c, dtype=float)
i,j = np.where(np.isclose(a, element))

such that i and j contain the line and column indices, respectively.这样ij分别包含行和列索引。

Example:例子:

a = np.array([[1, 2],
              [3, 4],
              [2, 6]], dtype=float)
i, j = np.where(np.isclose(a, 2))
print(i)
#array([0, 2])
print(j)
#array([1, 0]))

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

相关问题 如何搜索二维列表并打印位置(x,y) - How to search through a 2d list and prints the location (x, y) 使用 Python 压缩和列出函数来组合两个二维数组以生成 x,y 坐标? - Using Python to zip and list functions to combine two 2D arrays to produce x,y coordinates? 使用2D索引(x,y,z)的2D列表递增3D计数器数组:Python Numpy Slicing - Increment 3D array of counters from 2D list of indices (x,y,z) using: Python Numpy Slicing 在 2D 数组 Python 中查找值的位置(x 和 y 索引)(例如,在 limit=0.5 以上) - Find the location (x and y index) of values (for example above a limit=0.5) in an 2D array Python python 中的二维轮廓 plot 使用一维 X、Y 和 Z 变量 - 2D contour plot in python using 1D X, Y and Z variables Python - 给定 function 的值,查找二维高斯的 x 和 y 值 - Python - Find x and y values of a 2D gaussian given a value for the function 使用列表推导搜索二维数组(Python) - Using list comprehension to search a 2d array (python) 使用二维数组列表搜索 Function Python - Search Function Python using 2D array list 如何从python中的2D列表中获取最大x和最小y坐标? - How to get the max x and min y coordinates from a 2D list in python? Python-将2D坐标对列表转换为2个X和Y分量列表 - Python - Convert list of 2D co-ordinate pairs to 2 lists of X and Y components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM