[英]How to search a list for a pair of coordinates?
I have a list of lists containing some text data and numerical coordinates, as follows: 我有一个包含一些文本数据和数字坐标的列表列表,如下所示:
coords = [['1a', 'sp1', '1', '9'],
['1b', 'sp1', '3', '11'],
['1c', 'sp1', '6', '12'],
['2a', 'sp2', '1', '9'],
['2b', 'sp2', '1', '10'],
['2c', 'sp2', '3', '10'],
['2d', 'sp2', '4', '11'],
['2e', 'sp2', '5', '12'],
['2f', 'sp2', '6', '12'],
['3a', 'sp3', '4', '13'],
['3b', 'sp3', '5', '11'],
['3c', 'sp3', '8', '8'],
['4a', 'sp4', '4', '12'],
['4b', 'sp4', '6', '11'],
['4c', 'sp4', '7', '8'],
['5a', 'sp5', '8', '8'],
['5b', 'sp5', '7', '6'],
['5c', 'sp5', '8', '2'],
['6a', 'sp6', '8', '8'],
['6b', 'sp6', '7', '5'],
['6c', 'sp6', '8', '3']]
Given a pair of coordinates (x,y), I want to find the element in the list (which will be itself a list) corresponding to said pair of coordinates. 给定一对坐标(x,y),我想在列表中找到元素(其本身就是一个列表),该元素对应于所述一对坐标。 So, for example, if I have x = 5 and y = 12, I would get ['2e', 'sp2', '5', '12']
. 因此,例如,如果我有x = 5且y = 12,我将得到['2e', 'sp2', '5', '12']
。
I tried this: 我尝试了这个:
x = 5
y = 12
print coords[(coords == str(x)) & (coords == str(y))]
but got an empty list. 但有一个空列表。
I also tried this: 我也试过这个:
import numpy as np
print np.where(coords == str(x)) and np.where(coords == str(y))
but could not make any sense of what it returned ((array([ 2, 7, 8, 12]), array([3, 3, 3, 3])))
. 但对返回的内容毫无意义((array([ 2, 7, 8, 12]), array([3, 3, 3, 3])))
。
Could anyone give me a hand? 有人可以帮我吗?
Make use of list comprehension. 利用列表理解。 Iterate through all the coordinate and see where x and y are equal. 遍历所有坐标,并查看x和y相等的地方。
coords = [['1a', 'sp1', '1', '9'], ['1b', 'sp1', '3', '11'], ['1c', 'sp1', '6', '12'], ['2a', 'sp2', '1', '9'], ['2b', 'sp2', '1', '10'], ['2c', 'sp2', '3', '10'], ['2d', 'sp2', '4', '11'], ['2e', 'sp2', '5', '12'], ['2f', 'sp2', '6', '12'], ['3a', 'sp3', '4', '13'], ['3b', 'sp3', '5', '11'], ['3c', 'sp3', '8', '8'], ['4a', 'sp4', '4', '12'], ['4b', 'sp4', '6', '11'], ['4c', 'sp4', '7', '8'], ['5a', 'sp5', '8', '8'], ['5b', 'sp5', '7', '6'], ['5c', 'sp5', '8', '2'], ['6a', 'sp6', '8', '8'], ['6b', 'sp6', '7', '5'], ['6c', 'sp6', '8', '3']]
x = 5
y = 12
answer = [cood for cood in coords if int(cood[2]) == x and int(cood[3]) == y]
print(answer)
For a general solution you can use dictionary comprehension, 对于一般的解决方案,您可以使用字典理解,
x, y = 5, 12
print({tuple(coord[-2:]):coord for coord in coords}[str(x),str(y)])
If you are looking for a simple Python solution try using this 如果您正在寻找简单的Python解决方案,请尝试使用此方法
[coord for coord in coords if coord[2] == str(x) and coord[3] == str(y) ]
This does return you back [['2e', 'sp2', '5', '12']]
这确实使您返回[['2e', 'sp2', '5', '12']]
I'm not sure what you are trying to accomplish in your solution print coords[(coords == str(x)) & (coords == str(y))]
. 我不确定您要在解决方案中完成什么print coords[(coords == str(x)) & (coords == str(y))]
。 You need to iterate through the list to find which of the elements match your (x, y)
coordinates. 您需要遍历列表以查找哪些元素与(x, y)
坐标相匹配。
You could use this non-numpy list comprehension: 您可以使用以下非数字列表理解:
>>> [[a,b,c,d] for (a,b,c,d) in coords if int(c) == x and int(d) == y]
[['2e', 'sp2', '5', '12']]
Using numpy
, you should compare only the third and fourth column to x
and y
, not the entire row, and take those indices. 使用numpy
,您应该只将第三和第四列与x
和y
,而不是整个行,然后采用这些索引。
>>> arr = np.array(coords)
>>> arr[(arr[:,2] == str(x)) & (arr[:,3] == str(y))]
array([['2e', 'sp2', '5', '12']], dtype='|S3')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.