在列表中拉链计数
问题描述:
我在列表中遇到了一些问题。在列表中拉链计数
我正在做自我避免散步的随机行走,即你不能在同一个地方走过两次。 (0,0),(1,0),(2,0),(2,1),(2,3),(2),(2),(3) 0)....但这只是一个正常的随机游走,因为我可以重复坐标。我的想法是在循环中添加一个条件,但这里是我遇到问题的地方。
如何告诉我的代码以避免重复坐标?例如,我有
coords=zip(x,y)
for i in coords:
if i "is repeated":
"dont go to this coord"
else:
"go there".
这只是一个例子,我有写的代码“不要去这个坐标”和“去那里”,我只是需要帮助写的条件,“如果我是repeted”。
感谢
答
这是概念:
coords=zip(x,y)
used_coords = []
for i in coords:
if i in used_coords:
"dont go to this coord"
else:
"go there".
used_coords.append(i)
商店他们在一个数组,并检查是否'i'is该数组中? –