python - 用于嵌套列表中的列表的 if-in 语句测试无法识别第一个列表在函数中使用时位于第二个列表中
问题描述
if-in 语句应该打印 'test',但它不会。每当我在函数之外尝试类似的想法时,它都能完美运行,就像底部的示例一样。我可以说问题出在 if in 语句上,但我不明白为什么它不起作用。我能做些什么来解决这个问题吗?
class Pawn:
def __init__(self, num, pos, col, fmove):
self.num = num
self.pos = pos
self.col = col
self.fmove = fmove
w_pawn1 = Pawn(1, [1, 2], 'White', True)
def move(piece, typ):
if typ == 'pawn':
if piece.fmove == True:
posmove = [[piece.pos[0], int(piece.pos[1])+1],
[piece.pos[0], int(piece.pos[1])+2]]
Move = input('Where do you want to move?')
print(Move)
print(posmove)
if Move in posmove:
print('test')
piece.pos = move
else: pass
list1 = [1,3]
list2 = [[1,3],[1,4]]
if list1 in list2:
print('ok')
output:ok
解决方案
推荐阅读
- arrays - Rust 中“for in n..m”的意外行为
- node.js - 使用带有错误传递选项 { useUnifiedTopology: true } 的 nodejs 到 MongoClient 构造函数
- hyperledger-fabric - Hyperledger Fabric:无法从远程对等方获取私有数据
- windows-10 - Manjaro+Remmina+RDP (Win10) 打开新窗口时滞后3-5秒(如打开thunderbird、windows explorer等)
- django - CustomAuthBackend() 缺少 1 个必需的位置参数:'ModelBackend'
- swift - Swift:如何将数据添加到对象列表中的对象变量
- sql-server - 通过在 SQL 中具有组条件的 COUNT
- masstransit - 需要StateMachine的Activity例子
- selenium - 如何使用 appium 在浏览器堆栈中执行期间更新应用程序?
- python - 理解 tweepy 的状态对象