我正在运行for来检查元组列表。
中的内容for i in range of b:
actual=i
temp1=(actual[0]+1,actual[1])
temp2=(actual[0],actual[1]-1)
temp3=(actual[0],actual[1]+1)
temp4=(actual[0]-1,actual[1])
我想确保temp永远不会在之前的循环中验证元组的值。关于如何做到这一点的任何想法?
答案 0 :(得分:0)
首先,您的代码似乎存在问题。 range
接受整数输入,因此如果b
是整数,for i in range(b)
将在列表中为您提供整数[0, 1, 2, .. , b-1 ]
。您无法使用i
索引[]
,就像接下来的两行一样。
如果b
不是整数,而是集合,那么你应该使用类似的东西:
# Assuming b is a collection
for i in range(len(b)):
actual=b[i]
temp1=(actual[0]+1,actual[1])
temp2=(actual[0],actual[1]-1)
temp3=(actual[0],actual[1]+1)
temp4=(actual[0]-1,actual[1])
# Check if this is the first one. If it is, previous won't exist.
if i == 0:
continue
previous = b[i-1]
if previous in [ temp1, temp2, temp3, temp4 ]:
# This is what you want not to happen. Deal with it somehow.
pass
答案 1 :(得分:0)
这是我的两分钱。 请注意,如果匹配,这将使temp(1-4)无。
# assuming b is a collection
for i in range(len(b)):
actual=b[i]
if i!=0:
prev = b[i-1]
if i==0:
prev = [[['something']],[['ridiculous']]] #this is so that the rest of the code works even if index is 0
if (actual[0]+1,actual[1]) != prev: #if it is not the previous item
temp1=(actual[0]+1,actual[1]) #assign temp1
else:
temp1 = None #temp1 would otherwise automatically take on the value of (b[i-1][0]+1,b[i-1][1])
if (actual[0],actual[1]-1) != prev:
temp2=(actual[0],actual[1]-1)
else:
temp2 = None
if (actual[0],actual[1]+1) != prev:
temp3=(actual[0],actual[1]+1)
else:
temp3 = None
if (actual[0]-1,actual[1]) != prev:
temp4=(actual[0]-1,actual[1])
else:
temp4 = None