0
text = ''.join(sorted([x for x in input()]))
text = text.replace('+', '', text.count('+'))
我只是喜欢它,当你可以用Python在一行中做所有事情。如何将这两行代码合并为一个(Python 3.X)?
text = ''.join(sorted([x for x in input()]))
text = text.replace('+', '', text.count('+'))
我只是喜欢它,当你可以用Python在一行中做所有事情。如何将这两行代码合并为一个(Python 3.X)?
text = ''.join(sorted(input())).replace('+', '')
OR
text = ''.join(sorted(input().replace('+', '')))
input()
排序(); sorted
支持任何迭代。str.replace()
的第三个参数是多余的。因为代码正在替换所有发生的+
。好吧,这是不完全一样的代码,但在这种情况下,结果是相似的:
text = ''.join(sorted([x for x in input() if x != '+']))
而不是创建整个字符串,然后更换一个字符,你可以简单地删除它在第一个列表comperhesion。
你可以使用方法链接'https:// en.wikipedia.org/wiki/Method_chaining' –