python 中 while (cin >> var) 的等价物是什么?
What's the equivalent for while (cin >> var) in python?
在线比赛中,当没有指定输入长度,无法通过程序直接读取输入文件时,可以使用C++代码:
while (cin >> var)
{
//do something with var
}
python 的等价物是什么?
- 不使用任何与文件相关的函数,例如
open() write() ...
这可能会有帮助。
import sys
for line in sys.stdin:
#Do stuff
Python 中没有直接对应项。但是你可以用两个嵌套循环来模拟它:
for line in sys.stdin:
for var in line.split():
如果您需要的不是字符串,则需要在单独的步骤中进行转换:
var = int(var)
编写如下代码
while True:
a=input()
if(a==''):
break
else
.....
.
在其他部分你编写要执行的代码
如果您想在代码中使用 int 将其转换为 int 并使用它
在 C++ 中 cin >> n
具有双重性质:它既充当 布尔表达式 指示它是否已读取 (true) 或未读取 (false)序列中的元素,并作为获取元素的 "channel in" 运算符(如果有剩余的话)。遗憾的是,在 python 中,最接近开箱即用的方法是 n = input()
,它也可以作为 "channel in" ,但不能作为布尔表达式.
为了解决这个问题(通过 "C++onic" 而不是 "Pythonic"),您可以定义一个辅助函数,方便地称为 cin()
。该函数将有一个参数 n
,它实际上就像 C++ 传递引用一样工作(即 input/output 参数)。为了模拟这种行为,我在这里将 n
设为 具有一个元素的列表对象 - 在本例中为整数 - ,利用 Python 列表的别名特性。这样,辅助方法中n[0]
变化时,每次cin(n)
调用returnstrue时,主方法n[0]
值的变化也会体现出来。任何进一步做。
def cin(n):
#function that woks equally to doing cin >> n
try:
n[0] = int(input())
return True
except:
return False
def main():
#C++ like main method (for illustrative/comparative purposes)
n = [0]
while cin(n):
#MESSAGE: do someting with n[0] (watch out, n is list object with one element)
main()
例如,在前面的代码中,如果你想打印一个数字序列的元素的双倍,并且它们的数量不定,你只需将#MESSAGE行更改为以下内容:
print(n[0]*2)
或者,另一个例子,如果你想实现一个递归解决方案以逆序打印序列的元素(也具有不确定的长度)并且具有不使用包含多个元素的列表的约束,你会,同样,需要为以下内容更改 #MESSAGE 行:
main()
print(n[0])
std::cin
的一个关键功能是 it skips whitespace (competition programming problems are space and newline separated for this reason). I suggest reading and processing one line at a time like the other answers suggest, but it is possible to use str.split()
在生成器中 模拟 一次读取一个数字,就像 C++ std::cin >> n
用法:
import sys
def stdin_gen():
for x in sys.stdin.read().split():
yield int(x)
cin = stdin_gen()
x = next(cin)
y = next(cin)
print(x, y)
在线比赛中,当没有指定输入长度,无法通过程序直接读取输入文件时,可以使用C++代码:
while (cin >> var)
{
//do something with var
}
python 的等价物是什么?
- 不使用任何与文件相关的函数,例如
open() write() ...
这可能会有帮助。
import sys
for line in sys.stdin:
#Do stuff
Python 中没有直接对应项。但是你可以用两个嵌套循环来模拟它:
for line in sys.stdin:
for var in line.split():
如果您需要的不是字符串,则需要在单独的步骤中进行转换:
var = int(var)
编写如下代码
while True:
a=input()
if(a==''):
break
else
.....
.
在其他部分你编写要执行的代码 如果您想在代码中使用 int 将其转换为 int 并使用它
在 C++ 中 cin >> n
具有双重性质:它既充当 布尔表达式 指示它是否已读取 (true) 或未读取 (false)序列中的元素,并作为获取元素的 "channel in" 运算符(如果有剩余的话)。遗憾的是,在 python 中,最接近开箱即用的方法是 n = input()
,它也可以作为 "channel in" ,但不能作为布尔表达式.
为了解决这个问题(通过 "C++onic" 而不是 "Pythonic"),您可以定义一个辅助函数,方便地称为 cin()
。该函数将有一个参数 n
,它实际上就像 C++ 传递引用一样工作(即 input/output 参数)。为了模拟这种行为,我在这里将 n
设为 具有一个元素的列表对象 - 在本例中为整数 - ,利用 Python 列表的别名特性。这样,辅助方法中n[0]
变化时,每次cin(n)
调用returnstrue时,主方法n[0]
值的变化也会体现出来。任何进一步做。
def cin(n):
#function that woks equally to doing cin >> n
try:
n[0] = int(input())
return True
except:
return False
def main():
#C++ like main method (for illustrative/comparative purposes)
n = [0]
while cin(n):
#MESSAGE: do someting with n[0] (watch out, n is list object with one element)
main()
例如,在前面的代码中,如果你想打印一个数字序列的元素的双倍,并且它们的数量不定,你只需将#MESSAGE行更改为以下内容:
print(n[0]*2)
或者,另一个例子,如果你想实现一个递归解决方案以逆序打印序列的元素(也具有不确定的长度)并且具有不使用包含多个元素的列表的约束,你会,同样,需要为以下内容更改 #MESSAGE 行:
main()
print(n[0])
std::cin
的一个关键功能是 it skips whitespace (competition programming problems are space and newline separated for this reason). I suggest reading and processing one line at a time like the other answers suggest, but it is possible to use str.split()
在生成器中 模拟 一次读取一个数字,就像 C++ std::cin >> n
用法:
import sys
def stdin_gen():
for x in sys.stdin.read().split():
yield int(x)
cin = stdin_gen()
x = next(cin)
y = next(cin)
print(x, y)