简体   繁体   English

找到两个列表的所有组合

[英]find all combination of two lists

I have a question regarding the python.我有一个关于python的问题。 I have a grid like the attached figure.我有一个像附图一样的网格。 I have the coordinates of all states.我有所有州的坐标。 The coordinates of the states are shown in the A and B lists:状态的坐标显示在 A 和 B 列表中:

A = [1,2,3]
B = [4,5,6]

I want to find the coordinate of the state that I want.我想找到我想要的状态的坐标。 For example, I want to go to state (0,1) and I want its coordinate as y =(2,4).例如,我想转到状态 (0,1) 并且我希望它的坐标为 y =(2,4)。

在此处输入图片说明

My solution to this would be to create the following function:我对此的解决方案是创建以下函数:

def convert(state, A, B): # state should be of the shape (int, int)
   return (A[state[1]], B[state[0]])

There is no error checking here so make sure the indexes are within bounds.这里没有错误检查,因此请确保索引在范围内。

You can now use the function as so:您现在可以使用该函数:

A=[1,2,3]
B=[4,5,6]
sx = (0,1)
y = convert(sx, A, B)

I hope this helps我希望这有帮助

y=(A[1],B[0])

try list indexing
def getv(x,y):
  a = A.index(x)
  b = B.index(y)
  return (a,b)

getv(0,1)

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

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