无法检查数组中 Python 3 中的标准输入
Having trouble checking stdin in Python 3 in array
如果我这样做,它总是 returns x
false
。为什么会这样,请指教一下好吗?
import sys
for line in sys.stdin:
valid = ["()", "[]", "{}"]
x = True if line in valid else False
您需要使用str.rstrip
去除每行末尾的换行符:
import sys
# since there's one invariant `valid` list, define it here, outside the loop
valid = ["()", "[]", "{}"]
for line in sys.stdin:
x = line.rstrip() in valid # no need for if ... else
print(x)
将行更改为:
x = True if line.rstrip() in valid else False
如果我这样做,它总是 returns x
false
。为什么会这样,请指教一下好吗?
import sys
for line in sys.stdin:
valid = ["()", "[]", "{}"]
x = True if line in valid else False
您需要使用str.rstrip
去除每行末尾的换行符:
import sys
# since there's one invariant `valid` list, define it here, outside the loop
valid = ["()", "[]", "{}"]
for line in sys.stdin:
x = line.rstrip() in valid # no need for if ... else
print(x)
将行更改为:
x = True if line.rstrip() in valid else False