Python-nowcoder Zero-complexity Transposition
题目描述
You are given a sequence of integer numbers. Zero-complexity transposition of the sequence is the reverse of this sequence. Your task is to write a program that prints zero-complexity transposition of the given sequence.
输入描述:
For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).
输出描述:
For each case, on the first line of the output file print the sequence in the reverse order.
输入
5
-3 4 6 -8 9
输出
9 -8 6 4 -3
原思路:将首部和尾部元素进行调换,直至到中间元素为止(使用了列表输出,不符合题意)
错误之处有:
1、replace函数使用错误,会导致做元素替换事把11当作“1”,“1”来进行处理
2、使用列表对输出进行处理,即最后一行不应该为list类型,这样输出就有中括号,来瞅瞅这样就对了????
- replace 和 split的区别
- replace()是替换函数,两个参数,第一个参数是被替换内容,第二个参数是替换内容。即第二个替换第一个。
- split() 是分割函数,操作:a=a.split(’,’),a变成了列表[‘1’,‘2’,‘3’]
他人解答,看别人的解决办法也是一种学习的途径
如:直接利用Python的语法特性
try:
while 1:
n = input()
print(' '.join(input().split()[::-1]))
except:
pass
????但是输入n根本没有任何意义
如:使用一些内置函数
while True:
try:
n = int(input())
num = [int(i) for i in input().split(" ")]
num.reverse()
for i in range(0, n):
print(num[i], end=" ")
except:
break
知识点:
Python字符串与列表的相互转换
1、字符串转列表 split
2、列表转字符串 join
>>> str1 = "hi hello world"
>>> print(str1.split(" "))
['hi', 'hello', 'world']
>>> list1 = ['hi', 'hello', 'world']
>>> print(" ".join(list1))
hi hello world
>>>
Python实现字符串反转的几种方法
1、使用字符串切片
result = s[::-1]
s = 1, 2, 3, 4, 5
result = s[::-1]
print(result)
2、使用列表的reverse方法
l = list(s)
result = “”.join(l.reverse)
同理
l = list(s)
result = “”.join(l[::-1])
s = 1, 2, 3, 4, 5
l = list(s)
print(l)
l.reverse()
print(l)
result = " ".join('%s' %id for id in l)
print(result)