在Python中查找坐标的索引值
Find the index value of coordinates in Python
我有两个数组:
A = [[1,0],[2,5],[6,7],[3,6],[7,6],[0,2],[4,1],[9,3],[6,5],[5,8]]
B = [[6,7],[0,2],[6,5]]
首先,我想找到A的索引值,然后将其与B进行比较,并在B中打印索引值wrt A。
我期望的是:
[2, 5, 8]
如果我没理解错的话,你想找到 B 中也存在的 A 的索引?
在那种情况下:
C = [i for i in range(len(A)) if A[i] in B]
输出:
[2, 5, 8]
d = []
c = 0
for i in A:
if i in B:
d.append(c)
c +=1
print(d)
[2, 5, 8]
你也许可以这样做,这可能比 0(n^2)
快一点
mapper={}
for index,value in enumerate(B):
mapper[f'{value}']=index
# print(mapper)
ans=[]
for index,value in enumerate(A):
if f'{value}' in mapper.keys():
ans.append(index)
print(ans)
ans > [2, 5, 8]
我有两个数组:
A = [[1,0],[2,5],[6,7],[3,6],[7,6],[0,2],[4,1],[9,3],[6,5],[5,8]]
B = [[6,7],[0,2],[6,5]]
首先,我想找到A的索引值,然后将其与B进行比较,并在B中打印索引值wrt A。 我期望的是:
[2, 5, 8]
如果我没理解错的话,你想找到 B 中也存在的 A 的索引?
在那种情况下:
C = [i for i in range(len(A)) if A[i] in B]
输出:
[2, 5, 8]
d = []
c = 0
for i in A:
if i in B:
d.append(c)
c +=1
print(d)
[2, 5, 8]
你也许可以这样做,这可能比 0(n^2)
快一点mapper={}
for index,value in enumerate(B):
mapper[f'{value}']=index
# print(mapper)
ans=[]
for index,value in enumerate(A):
if f'{value}' in mapper.keys():
ans.append(index)
print(ans)
ans > [2, 5, 8]