Python:你究竟如何取一个字符串,将其拆分、反转并重新连接在一起?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3627270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:59:36  来源:igfitidea点击:

Python: How exactly can you take a string, split it, reverse it and join it back together again?

pythonstringjoinsplitreverse

提问by Tstrmwarrior

How exactly can you take a string, split it, reverse it and join it back together again without the brackets, commas, etc. using python?

使用python,如何在没有括号、逗号等的情况下取出一个字符串、拆分它、反转它并将其重新连接在一起?

采纳答案by Mad Scientist

>>> tmp = "a,b,cde"
>>> tmp2 = tmp.split(',')
>>> tmp2.reverse()
>>> "".join(tmp2)
'cdeba'

or simpler:

或更简单:

>>> tmp = "a,b,cde"
>>> ''.join(tmp.split(',')[::-1])
'cdeba'

The important parts here are the split functionand the join function. To reverse the list you can use reverse(), which reverses the list in place or the slicing syntax [::-1]which returns a new, reversed list.

这里的重要部分是split 函数join 函数。要反转列表,您可以使用reverse(),它将列表反转到位,或者使用切片语法[::-1]返回一个新的反转列表。

回答by unutbu

Do you mean like this?

你的意思是这样吗?

import string
astr='a(b[c])d'

deleter=string.maketrans('()[]','    ')
print(astr.translate(deleter))
# a b c  d
print(astr.translate(deleter).split())
# ['a', 'b', 'c', 'd']
print(list(reversed(astr.translate(deleter).split())))
# ['d', 'c', 'b', 'a']
print(' '.join(reversed(astr.translate(deleter).split())))
# d c b a

回答by Tony Veijalainen

You mean this?

你是这个意思?

from string import punctuation, digits

takeout = punctuation + digits

turnthis = "(fjskl) 234 = -345 089 abcdef"
turnthis = turnthis.translate(None, takeout)[::-1]
print turnthis

回答by Ruman Khan

I was asked to do so without using any inbuilt function. So I wrote three functions for these tasks. Here is the code-

我被要求在不使用任何内置函数的情况下这样做。所以我为这些任务写了三个函数。这是代码-

def string_to_list(string):
'''function takes actual string and put each word of string in a list'''
list_ = []
x = 0 #Here x tracks the starting of word while y look after the end of word.
for y in range(len(string)):
    if string[y]==" ":
        list_.append(string[x:y])
        x = y+1
    elif y==len(string)-1:
        list_.append(string[x:y+1])
return list_

def list_to_reverse(list_):
'''Function takes the list of words and reverses that list'''
reversed_list = []
for element in list_[::-1]:
    reversed_list.append(element)
return reversed_list

def list_to_string(list_):
'''This function takes the list and put all the elements of the list to a string with 
space as a separator'''
final_string = str()
for element in list_:
    final_string += str(element) + " "
return final_string

#Output
text = "I love India"
list_ = string_to_list(text)
reverse_list = list_to_reverse(list_)
final_string = list_to_string(reverse_list)
print("Input is - {}; Output is - {}".format(text, final_string))
#op= Input is - I love India; Output is - India love I 

Please remember, This is one of a simpler solution. This can be optimized so try that. Thank you!

请记住,这是一种更简单的解决方案。这可以优化,所以试试吧。谢谢!