如何int从输入中读取N s,并在找到时停止读取\n?另外,如何将它们添加到我可以使用的数组中?
我在C中寻找类似的东西,但在python中
while(scanf("%d%c",&somearray[i],&c)!=EOF){
i++;
if (c == '\n'){
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*let 17
在Python 2中:
lst = map(int, raw_input().split())
Run Code Online (Sandbox Code Playgroud)
raw_input()从输入中读取整行(在此处停止\n)作为字符串.
.split()通过将输入拆分为单词来创建字符串列表.
map(int, ...)从这些单词创建整数.
在Python 3 raw_input中,已经重命名input并map返回迭代器而不是列表,因此需要进行一些更改:
lst = list(map(int, input().split()))
Run Code Online (Sandbox Code Playgroud)
Joh*_*ooy 13
在Python中没有直接等效的scanf,但这应该有效
somearray = map(int, raw_input().split())
Run Code Online (Sandbox Code Playgroud)
在Python3 raw_input中已经重命名为input
somearray = map(int, input().split())
Run Code Online (Sandbox Code Playgroud)
这是一个细分/解释
>>> raw=raw_input() # raw_input waits for some input
1 2 3 4 5 # I entered this
>>> print raw
1 2 3 4 5
>>> print raw.split() # Make a list by splitting raw at whitespace
['1', '2', '3', '4', '5']
>>> print map(int, raw.split()) # map calls each int() for each item in the list
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)