题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
while True:
try:
string = input().strip()
for word in string:
if not word.isalpha():
string = string.replace(word, " ")// 将非alpha的字符用空格取代
string =string.split(" ")//切割后形成数组(多空格的话里面也有空字符)
new_str = list(filter(None,string))// filter(None,string)可过滤掉空字符
print(" ".join(new_str[::-1]))
except:
break
消除数组空字符的方法:
- filter(None, your_list)
while '' in your_list:
your_list.remove(' ')
[x for x in your_list if x!='']