2011-01-18 12 views
0

imはタプルのリストをチェックするために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]) 

のラインで何か、私は一時のNEVERは、前のサイクルで検証タプルの値を取ることを確認します。どのようにこれを行う上の任意のアイデア?

+0

あなたの質問は曖昧です。どのようにタプルを "検証"していますか?前のループで何を確認しているのかわからないので、このループの一部であるコードを投稿してください。 – inspectorG4dget

+0

このコードは実際に実行されますか? – fmark

+0

あなたは閉鎖を心配していますか?それはあなたの質問が関連しているのでしょうか? –

答えて

0

まず、コードに問題があるようです。 rangeは整数入力を受け入れますので、bが整数の場合、for i in range(b)は整数を[0, 1, 2, .. , b-1 ]にします。次の2行で行うように、[]を使用して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 
0

ここに私の2セントです。 これは、一致するものがあればtemp(1-4)Noneにすることに注意してください。

# 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 
関連する問題