limit = raw_input("Enter amount of numbers: ")
if limit.decode("utf-8").isdecimal(): # check if string contains only digits
inp = raw_input("Enter the numbers: ")
if int(limit) <= inp.count(" ") + 1: # amount of spaces + 1 is amount of numbers
a = map(int, inp.split(" ")) # split string and convert str to int
b = map(sum, zip(a, reversed(a))) # sum elements of input list and reversed
res = " ".join(map(str, b)) # convert result to str and join to string
代码(python 3):
limit = input("Enter amount of numbers: ")
if limit.isdecimal():
inp = input("Enter the numbers: ")
if int(limit) <= inp.count(" ") + 1:
a = list(map(int, inp.split(" ")))
b = list(map(sum, zip(a, reversed(a))))
res = " ".join(map(str, b))
要从用空格分隔的数字字符串中获取列表,可以使用^{} 。要遍历列表,可以使用^{} 。^{} 用于从头到尾遍历列表,^{} -将原始列表中的元素连接起来并将结果传递给^{} 。^{} 用提供的分隔符连接列表的元素。你知道吗
代码(python 2):
代码(python 3):
我想有很多方法可以解决这个问题。 也许你可以试试这个。你知道吗
或者只是
随便问吧。你知道吗
您可以这样做:
相关问题 更多 >
编程相关推荐