所以我被传递了一串整数。第一个表示我正在传递的整数数量,其余的是正在传递的整数。
我正在传递的字符串示例:4 1 2 3 4 预期输出:4 3 2 1
这是我写的
import sys
n = int(raw_input().strip())
arr = map(int,raw_input().strip().split(' '))
arr.reverse()
print(arr)
如果我输入 4 然后输入,然后 1 2 3 4,它可以工作,但如果我输入 4 1 2 3 4 然后按 Enter,它就不起作用我收到此错误:
Traceback (most recent call last):
*File name removed by me* line 4, in <module>
n = int(raw_input().strip())
ValueError: invalid literal for int() with base 10: '4 1 2 3 4'
我理解这个错误...它期望 n = int(raw_input().strip()) 的单个基数 10 数字,而不是由空格分隔的数字字符串。我试图改变我的代码以几乎完全否定第一个数字......它甚至没有必要,我认为就Python而言没有必要。有人可以帮忙吗?我觉得我还差一点点。
最佳答案
I have tried to alter my code to almost negate the first number entirely... its not even necessary I don't think as far as python goes.
你是对的,你可以将列表从第二个元素分割到最后一个元素,忽略第一个元素。
arr = map(int,raw_input().strip().split(' ')[1:])
关于python - 一串输入 : Ints seperated by ' ' need to print array in reverse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37063592/