如何将这两行代码合并为一行(Python3.X)?

如何将这两行代码合并为一行(Python3.X)?,python,python-3.x,sorting,join,replace,Python,Python 3.x,Sorting,Join,Replace,我只是喜欢用Python在一行中完成所有事情 text = ''.join(sorted([x for x in input()])) text = text.replace('+', '', text.count('+')) 或 您不需要使用列表理解。只需将input()传递给sorted()排序支持任何iterable str.replace()的第三个参数是多余的。因为代码正在替换所有出现的+ 或 您不需要使用列表理解。只需将input()传递给sorted()排序支持任何iter

我只是喜欢用Python在一行中完成所有事情

text = ''.join(sorted([x for x in input()]))
text = text.replace('+', '', text.count('+'))

  • 您不需要使用列表理解。只需将
    input()
    传递给sorted()<代码>排序支持任何iterable
  • str.replace()
    的第三个参数是多余的。因为代码正在替换所有出现的
    +

  • 您不需要使用列表理解。只需将
    input()
    传递给sorted()<代码>排序支持任何iterable
  • str.replace()
    的第三个参数是多余的。因为代码正在替换所有出现的
    +

    • 好的,这不是完全相同的代码,但在这种情况下,结果类似:

      text = ''.join(sorted(input().replace('+', '')))
      

      与创建整个字符串然后替换一个字符不同,您只需在第一个列表ComperVersion中将其删除即可。

      好的,这段代码并不完全相同,但在本例中,结果类似:

      text = ''.join(sorted(input().replace('+', '')))
      

      您不必创建整个字符串然后替换一个字符,只需在第一个列表ComperVersion中将其删除。

      您可以使用方法链接
      https://en.wikipedia.org/wiki/Method_chaining
      您可以使用方法链接
      https://en.wikipedia.org/wiki/Method_chaining
      text = ''.join(sorted([x for x in input() if x != '+']))