現在のリストの各タプルの最後の要素に基づいてタプルのリストをいくつかの条件と比較しようとしています。最後の要素に基づくタプルのリストを条件と比較する方法
index = [
([('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))], (1, 0)),
([('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))], (1, 1)),
([('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))], (1, 1)),
([('T', (0, 3)), ('M', (1, 3)), ('T', (2, 3))], (1, 1)),
([('T', (0, 4)), ('T', (1, 4)), ('T', (2, 4))], (1, 0))
]
予想される出力は次のとおりです:
だから、私は、リストのこの種まし
[
[
[('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))],
[('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))]
],
[
[('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))]
],
[
[('T', (0, 3)), ('M', (1, 3)), ('T', (2, 3))],
[('T', (0, 4)), ('T', (1, 4)), ('T', (2, 4))]
]
]
私は何をしようとしているが、タプルの各最後の要素を比較することですし、いくつかの条件を適用する。
条件は以下のとおりです。
Compare Position k and k+1:
1- if i found ((1,0) and (1,1)) or ((0,1) and (1,0))
or ((1,1) and (1,0)) or ((1,1) and (0,1))
=> append to a list called `f_m` and go to `Position k+2`
2- if i found (1,1) and (1,1)
=> append to a list called `f_s` and go to `Position k+1`
と私がこれまで試したどのような0 <= i < len(index)
、しかしが偽出力である:
def get_final(index, step = 2):
k, i = 0, 0
f_m, f_s = [], []
while k < len(index):
while i <= step:
cond1 = (index[k+i][1][0] == 1 and index[k+i][1][1] == 0) and index[k+i+1][1][1] == 1
cond2 = (index[k+i][1][0] == 0 and index[k+i][1][1] == 1) and index[k+i+1][1][0] == 0
cond3 = (index[k+i][1][0] == 1 and index[k+i][1][1] == 1) and index[k+i+1][1][0] == 0
if cond1:
f_m += [index[k+i][0], index[k+i+1][0]]
i += 2
elif cond2:
f_m += [index[k+i][0], index[k+i+1][0]]
i += 2
elif cond3:
f_m += [index[k+i][0], index[k+i+1][0]]
i += 2
elif not cond1 and not cond2 and not cond3:
f_s += index[k+i][0]
i+=1
k+=i
print("f_m: ", f_m)
print("f_s: ", f_s)
私の偽の出力は次のとおりです。
f_m: [
[('T', (0, 0)), ('T', (1, 0)), ('T', (2, 0))],
[('T', (0, 1)), ('M', (1, 1)), ('T', (2, 1))]
]
f_s: [('T', (0, 2)), ('M', (1, 2)), ('T', (2, 2))]
Downvotersあなたは質問が悪いのかを説明できますか? –
ここでステップサイズは意味がないと思います。ステップサイズを変更すると、 'i = i + step'の条件も変わるためです。したがって、動的ではないパラメータも渡す必要があります。 – MYGz
@MYGzご意見ありがとうございます。私はそれを理解しようとします。あなたの答えとヒントのおかげで、ありがとう。 –