i came across a problem in Python and since i do this for fun and not as a professional i dont get why this does not work. I have a list that contains other lists with two numbers in each one. This function should check wether the element +1 and the element -1 are elements of my tp list. If yes, append them to stack. Code:
def check():
tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]]
stack = []
for i in tp:
a = i[0]
b = i[1]
if [(a - 1), (b - 1)] and [(a + 1), (b + 1)] in tp:
stack.append(i)
return stack
Unfortunately the output is:
[[0, 1], [1, 1], [1, 2], [2, 2]]
[1, 2] is correct because [0, 1] and [2, 3] are elements of tp.
[2, 2] is correct because [1, 1] and [3, 3] are elements of tp.
Why does this function give me the other two also? For instance: first element of tp is [0,1] -> [-1,0] and [1,2] should be the calculated outputs but obviously [-1,0] is no in this list. Where is my (probably obvious) mistake? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire