Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python在循环中获取空间分隔的输入_Python - Fatal编程技术网

使用python在循环中获取空间分隔的输入

使用python在循环中获取空间分隔的输入,python,Python,我还在学习python,所以这可能看起来很愚蠢。 这是C++代码我想要的python相当于: int t; for(int i=0;i<n;i++) { cin>>t; ans = do_something(t); } cout<<ans; ` 或者您可以在单行中执行此操作,如下所示: for i in map(int,raw_input().split()): ans = do_something(i) print ans 对于循环、原

我还在学习python,所以这可能看起来很愚蠢。 这是C++代码我想要的python相当于:

int t;
for(int i=0;i<n;i++)
{
    cin>>t;
    ans = do_something(t);
}
cout<<ans;
`

或者您可以在单行中执行此操作,如下所示:

for i in map(int,raw_input().split()):
    ans = do_something(i)
print ans

对于循环、原始输入和打印显示,您应该首先学习python,-do_somthing()在做什么?
input_from_user = raw_input() #which is a string '1 2 3 4 5'
numbers = input_from_user.split()
>>> ['1', '2', '3', '4', '5']
numbers = map(int, numbers) #To convert the string elements to integers.
>>> [1, 2, 3, 4, 5]

for i in numbers:
    ans = do_something(i)
print ans
for i in map(int,raw_input().split()):
    ans = do_something(i)
print ans