【问题标题】:Python: How exactly can you take a string, split it, reverse it and join it back together again?Python:你如何准确地获取一个字符串,将其拆分、反转并重新组合在一起?
【发布时间】:2011-04-07 08:04:45
【问题描述】:

如何使用 python 获取字符串、拆分、反转并再次将其重新组合在一起,而无需使用括号、逗号等?

【问题讨论】:

  • 你在哪个字符上分割字符串?空间?
  • 某处有一个 Missy Elliott 的笑话。
  • @detly tiesreverdnatitilpsnwodgnirtsymtupi?通过“iputmystringdownsplititandreverseit”[::-1] XD 生成

标签: python string join split reverse


【解决方案1】:
>>> tmp = "a,b,cde"
>>> tmp2 = tmp.split(',')
>>> tmp2.reverse()
>>> "".join(tmp2)
'cdeba'

或更简单:

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

这里的重要部分是split functionjoin function。要反转列表,您可以使用reverse(),它会在原地反转列表,或者使用切片语法[::-1],它会返回一个新的反转列表。

【讨论】:

  • 或者,''.join(reversed(tmp.split(','))),更明确一点。
【解决方案2】:

你的意思是这样吗?

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

【讨论】:

    【解决方案3】:

    你是说这个吗?

    from string import punctuation, digits
    
    takeout = punctuation + digits
    
    turnthis = "(fjskl) 234 = -345 089 abcdef"
    turnthis = turnthis.translate(None, takeout)[::-1]
    print turnthis
    

    【讨论】:

      【解决方案4】:

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

      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 
      

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

      【讨论】:

        【解决方案5】:

        不是 100% 适合这个特定的问题,但如果你想从后面分开,你可以这样做:

        theStringInQuestion[::-1].split('/', 1)[1][::-1]
        

        此代码从后面在符号“/”处拆分一次。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-09
          • 2018-07-05
          • 2013-09-12
          • 2021-11-16
          • 1970-01-01
          相关资源
          最近更新 更多