I have a list of tuples in Python, and I have a conditional where I want to take the branch ONLY if the tuple is not in the list (if it is in the list, then I don't want to take the if branch)
我在Python中有一个元组的列表,我有一个条件,我只想在tuple不在列表中(如果它在列表中,那么我不想使用if分支)来获取分支。
if curr_x -1 > 0 and (curr_x-1 , curr_y) not in myList:
# Do Something
This is not really working for me though. What have I done wrong?
这对我来说并不是真正的工作。我做错了什么?
2 个解决方案
#1
275
The bug is probably somewhere else in your code, because it should work fine:
bug可能在代码中的其他地方,因为它应该可以正常工作:
>>> 3 not in [2, 3, 4]
False
>>> 3 not in [4, 5, 6]
True
Or with tuples:
或元组:
>>> (2, 3) not in [(2, 3), (5, 6), (9, 1)]
False
>>> (2, 3) not in [(2, 7), (7, 3), "hi"]
True
#2
-1
a = [23, 11, 21, 34, 53, 89, 133, 211, 345, 535, 895]
b = [11, 32, 33, 45, 25, 66, 87, 863, 97, 130, 141, 126, 13]
x=[]
for i in a:
for j in b:
if i==j and j not in x:
x.append(j)
print(x)
#1
275
The bug is probably somewhere else in your code, because it should work fine:
bug可能在代码中的其他地方,因为它应该可以正常工作:
>>> 3 not in [2, 3, 4]
False
>>> 3 not in [4, 5, 6]
True
Or with tuples:
或元组:
>>> (2, 3) not in [(2, 3), (5, 6), (9, 1)]
False
>>> (2, 3) not in [(2, 7), (7, 3), "hi"]
True
#2
-1
a = [23, 11, 21, 34, 53, 89, 133, 211, 345, 535, 895]
b = [11, 32, 33, 45, 25, 66, 87, 863, 97, 130, 141, 126, 13]
x=[]
for i in a:
for j in b:
if i==j and j not in x:
x.append(j)
print(x)