Python 如何在列表中搜索一对坐标?
我有一个列表,其中包含一些文本数据和数字坐标,如下所示:Python 如何在列表中搜索一对坐标?,python,list,search,Python,List,Search,我有一个列表,其中包含一些文本数据和数字坐标,如下所示: 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'],
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,y),我想在列表中找到对应于这对坐标的元素(它本身就是一个列表)。例如,如果x=5,y=12,我会得到['2e','sp2','5','12']
我试过这个:
x = 5
y = 12
print coords[(coords == str(x)) & (coords == str(y))]
import numpy as np
print np.where(coords == str(x)) and np.where(coords == str(y))
但却有一张空名单
我也试过:
x = 5
y = 12
print coords[(coords == str(x)) & (coords == str(y))]
import numpy as np
print np.where(coords == str(x)) and np.where(coords == str(y))
但是无法理解它返回了什么((数组([2,7,8,12]),数组([3,3,3,3]))
谁能帮我一下吗?利用列表理解。遍历所有坐标并查看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)
利用列表理解。遍历所有坐标并查看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)
如果您正在寻找一个简单的Python解决方案,请尝试使用
[coord for coord in coords if coord[2] == str(x) and coord[3] == str(y) ]
这确实会让您返回[[2e',sp2',5',12']]
我不确定您在解决方案中试图实现什么
print coords[(coords==str(x))&(coords==str(y))]
。您需要遍历该列表,以找到与您的(x,y)
坐标匹配的元素。如果您正在寻找一个简单的Python解决方案,请尝试使用此
[coord for coord in coords if coord[2] == str(x) and coord[3] == str(y) ]
这确实会让您返回[[2e',sp2',5',12']]
我不确定您在解决方案中试图实现什么
print coords[(coords==str(x))&(coords==str(y))]
。您需要遍历列表,以找到哪些元素与您的(x,y)
坐标匹配。您可以使用此非numpy列表理解:
>>> [[a,b,c,d] for (a,b,c,d) in coords if int(c) == x and int(d) == y]
[['2e', 'sp2', '5', '12']]
使用numpy
,您应该只将第三列和第四列与x
和y
进行比较,而不是整行,并获取这些索引
>>> arr = np.array(coords)
>>> arr[(arr[:,2] == str(x)) & (arr[:,3] == str(y))]
array([['2e', 'sp2', '5', '12']], dtype='|S3')
您可以使用以下非numpy列表:
>>> [[a,b,c,d] for (a,b,c,d) in coords if int(c) == x and int(d) == y]
[['2e', 'sp2', '5', '12']]
使用numpy
,您应该只将第三列和第四列与x
和y
进行比较,而不是整行,并获取这些索引
>>> arr = np.array(coords)
>>> arr[(arr[:,2] == str(x)) & (arr[:,3] == str(y))]
array([['2e', 'sp2', '5', '12']], dtype='|S3')
对于一般解决方案,您可以使用字典理解
x, y = 5, 12
print({tuple(coord[-2:]):coord for coord in coords}[str(x),str(y)])
对于一般解决方案,您可以使用字典理解
x, y = 5, 12
print({tuple(coord[-2:]):coord for coord in coords}[str(x),str(y)])
list
对象不像numpy.ndarray
对象那样工作。我已经理解了这一点。对工作解决方案有任何建议吗?在列表上循环,检查sub[-2]==x和sub[-1]==y
list
对象是否与numpy.ndarray
对象不一样。我已经理解了这一点。对有效的解决方案有什么建议吗?在你的列表上循环,检查是否sub[-2]==x和sub[-1]==y
就是这样!非常感谢,就这样!非常感谢。非numpy解决方案也是一个不错的解决方案,但它只返回第一个列表元素(可能有重复的)。@maurobio为什么只返回第一个?如果有多个元素与模式匹配,它将返回所有元素。当然,我道歉。在运行代码之前,我只根据您提供的示例写了我的评论。非numpy解决方案也是一个不错的解决方案,但它只返回第一个列表元素(可能有重复的)。@maurobio为什么只返回第一个?如果有多个元素与模式匹配,它将返回所有元素。当然,我道歉。在运行代码之前,我仅根据您提供的示例编写了我的评论。这也是一个简单明了的解决方案。您在我的问题中指出的程序语句(print coords[(coords==str(x))&(coords==str(y)))显然是错误的(这就是它返回空列表的原因)。这也是一个简单而直接的解决方案。您在我的问题中指出的程序语句(print coords[(coords==str(x))&(coords==str(y)))显然有错误(这就是它返回空列表的原因)。请注意,如果列表中有多个匹配项,这将只返回最后一个。请注意,如果列表中有多个匹配项,这将只返回最后一个。