【问题标题】:To reverse sentence in python [duplicate]在python中反转句子[重复]
【发布时间】:2017-05-19 23:30:48
【问题描述】:

感谢您的所有回答。我知道为什么我的代码错误了。

请不要为我提供解决方案,因为我确实有一些解决方案,但我想了解我的代码不起作用。

我认为这是由于while x <= -len(split_result):" 但后来我认为逻辑是正确的。我的代码有什么问题?

O_string = ("Me name is Mr_T")
split_result = O_string.split()
print(split_result)

x=0
list=[]

while x <= -len(split_result):
    list.append(split_result[x-1])
    x = x-1

result=" ".join(list)
print (result)

【问题讨论】:

  • ' '.join('Hello world'.split()[::-1])
  • 在初始化为x=0 后,您正在检查x 是否小于或等于负值,因此您永远不会进入您的while 循环
  • 这是一个问题的副本,该问题询问如何反转 stringcharacters 的顺序。但是,这个问题是不同的:它询问如何反转 sentencewords 的顺序(由空格字符分隔)。

标签: python loops indexing


【解决方案1】:

可以使用reverse函数以及str.join

O_string = ("Me name is Mr_T")
split_result = O_string.split()
split_result.reverse()
print " ".join(split_result)

【讨论】:

    【解决方案2】:

    你可以试试:

    >>> O_string = ("Me name is Mr_T")
    >>> O_list = O_string.split()
    >>> O_list.reverse()
    >>> " ".join(O_list)
    'Mr_T is name Me'
    

    【讨论】:

      【解决方案3】:

      您可以使用[::-1] 反转列表:

      print(' '.join(O_string.split()[::-1]))
      

      输出:

      'Mr_T is name Me'
      

      这里的[::-1] 表示从头到尾的所有内容,步长为负一。

      或者,您可以使用内置函数reversed

      >>> ' '.join(reversed(O_string.split()))
      'Mr_T is name Me'
      

      关于您的算法。在我看来,在负指数中思考总是更困难。我建议积极:

      O_string = ("Me name is Mr_T")
      split_result = O_string.split()
      
      res = []
      x = len(split_result) - 1
      while x >= 0:
          res.append(split_result[x])
          x = x-1
      
      result=" ".join(res)
      print (result) 
      

      输出:

      'Mr_T is name Me'
      

      这里:

      x = len(split_result) - 1
      

      为您提供列表的最后一个索引。我们以0 开始索引。所以你需要从列表的长度中减去1

      你倒数:

      x = x-1
      

      一旦你得到一个负索引就停止:

      while x >= 0:
      

      提示:不要使用list 作为变量名。它是内置的,最好不要用于命名自己的对象。如果这样做,您将无法再轻松使用list()(在同一个命名空间中)。

      【讨论】:

        【解决方案4】:

        你可以使用reverse来反转句子:

        o_string = "Me name is Mr_T"
        words = sentence.split()
        o_string = " ".join(reversed(words))
        print(o_string)
        

        【讨论】:

          【解决方案5】:

          在 python 中,list[::-1] 会给你一个列表,列表的所有元素都存储在反向索引位置。即)reverseList1=list1[::-1] 使用这个。

          【讨论】:

            【解决方案6】:

            要回答“我的代码有什么问题?”,让我们看看在 shell 中发生了什么:

            >>> O_string = ("Me name is Mr_T")
            split_result = O_string.split()
            print(split_result)
            ['Me', 'name', 'is', 'Mr_T']
            >>> x=0
            >>> len(split_result)
            4
            >>> -len(split_result)
            -4
            >>> x
            0
            >>> 0 <= 4
            True
            >>> 0 <= -4
            False
            >>> x <= -len(split_result)
            False
            >>> while False: print('this will not get printed')
            

            因此,您的 while 循环条件永远不会为真,循环也永远不会发生。这是一个有效的例子:

            x = -1
            while x >= -len(split_result):
                list.append(split_result[x])
            

            【讨论】:

            • 说我一开始就设置x = 0,
            • @Alexis 那么你必须进行其他调整来补偿:x &gt;= -len(split_result) + 1split_result[x-1]
            猜你喜欢
            • 2020-12-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-11
            • 1970-01-01
            相关资源
            最近更新 更多