问题:如何在Python中按字母顺序对字符串中的字母进行排序
有没有一种简单的方法可以在Python中按字母顺序对字符串中的字母进行排序?
因此对于:
a = 'ZENOVW'
我想返回:
'ENOVWZ'
Is there an easy way to sort the letters in a string alphabetically in Python?
So for:
a = 'ZENOVW'
I would like to return:
'ENOVWZ'
回答 0
你可以做:
>>> a = 'ZENOVW'
>>> ''.join(sorted(a))
'ENOVWZ'
You can do:
>>> a = 'ZENOVW'
>>> ''.join(sorted(a))
'ENOVWZ'
回答 1
>>> a = 'ZENOVW'
>>> b = sorted(a)
>>> print b
['E', 'N', 'O', 'V', 'W', 'Z']
sorted
返回一个列表,这样你就可以用它做一个字符串,再次join
:
>>> c = ''.join(b)
其中将的项目b
与''
每个项目之间的空字符串连接在一起。
>>> print c
'ENOVWZ'
>>> a = 'ZENOVW'
>>> b = sorted(a)
>>> print b
['E', 'N', 'O', 'V', 'W', 'Z']
sorted
returns a list, so you can make it a string again using join
:
>>> c = ''.join(b)
which joins the items of b
together with an empty string ''
in between each item.
>>> print c
'ENOVWZ'
回答 2
Sorted()解决方案可以为您提供其他字符串带来的意外结果。
其他解决方案列表:
对字母排序并使其与众不同:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(set(s.lower())))
' belou'
排序字母并使它们与众不同,同时保持大写字母:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(set(s)))
' Bbelou'
排序字母并保留重复项:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(s))
' BBbbbbeellou'
如果要消除结果中的空间,请在上述任何情况下添加strip()函数:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(set(s.lower()))).strip()
'belou'
Sorted() solution can give you some unexpected results with other strings.
List of other solutions:
Sort letters and make them distinct:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(set(s.lower())))
' belou'
Sort letters and make them distinct while keeping caps:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(set(s)))
' Bbelou'
Sort letters and keep duplicates:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(s))
' BBbbbbeellou'
If you want to get rid of the space in the result, add strip() function in any of those mentioned cases:
>>> s = "Bubble Bobble"
>>> ''.join(sorted(set(s.lower()))).strip()
'belou'
回答 3
您可以使用减少
>>> a = 'ZENOVW'
>>> reduce(lambda x,y: x+y, sorted(a))
'ENOVWZ'
You can use reduce
>>> a = 'ZENOVW'
>>> reduce(lambda x,y: x+y, sorted(a))
'ENOVWZ'
回答 4
Python函数sorted
返回基于ASCII的字符串结果。
不正确:在下面的例子中,e
并且d
是落后H
和W
由于它以ASCII值。
>>>a = "Hello World!"
>>>"".join(sorted(a))
' !!HWdellloor'
正确:为了写排序后的字符串而不更改字母大小写。使用代码:
>>> a = "Hello World!"
>>> "".join(sorted(a,key=lambda x:x.lower()))
' !deHllloorW'
如果要删除所有标点和数字。使用代码:
>>> a = "Hello World!"
>>> "".join(filter(lambda x:x.isalpha(), sorted(a,key=lambda x:x.lower())))
'deHllloorW'
Python functionsorted
returns ASCII based result for string.
INCORRECT: In the example below, e
and d
is behind H
and W
due it’s to ASCII value.
>>>a = "Hello World!"
>>>"".join(sorted(a))
' !!HWdellloor'
CORRECT: In order to write the sorted string without changing the case of letter. Use the code:
>>> a = "Hello World!"
>>> "".join(sorted(a,key=lambda x:x.lower()))
' !deHllloorW'
If you want to remove all punctuation and numbers.
Use the code:
>>> a = "Hello World!"
>>> "".join(filter(lambda x:x.isalpha(), sorted(a,key=lambda x:x.lower())))
'deHllloorW'
回答 5
该代码可用于按字母顺序对字符串进行排序,而无需使用python的任何内置函数
k =输入(“再次输入任何字符串”)
li = []
x = len(k)
for i in range (0,x):
li.append(k[i])
print("List is : ",li)
for i in range(0,x):
for j in range(0,x):
if li[i]<li[j]:
temp = li[i]
li[i]=li[j]
li[j]=temp
j=""
for i in range(0,x):
j = j+li[i]
print("After sorting String is : ",j)
the code can be used to sort string in alphabetical order without using any inbuilt function of python
k = input(“Enter any string again “)
li = []
x = len(k)
for i in range (0,x):
li.append(k[i])
print("List is : ",li)
for i in range(0,x):
for j in range(0,x):
if li[i]<li[j]:
temp = li[i]
li[i]=li[j]
li[j]=temp
j=""
for i in range(0,x):
j = j+li[i]
print("After sorting String is : ",j)
回答 6
真的很喜欢用reduce()函数的答案。这是使用accumulate()对字符串排序的另一种方法。
from itertools import accumulate
s = 'mississippi'
print(tuple(accumulate(sorted(s)))[-1])
排序-> [‘i’,’i’,’i’,’i’,’m’,’p’,’p’,’s’,’s’,’s’,’s’ ]
元组(累计(已排序)->(’i’,’ii’,’iii’,’iiii’,’iiiim’,’iiiimp’,’iiiimpp’,’iiiimpps’,’iiiimppss’,’iiiimppsss ‘,’iiiimppssss’)
我们正在选择元组的最后一个索引(-1)
Really liked the answer with the reduce() function. Here’s another way to sort the string using accumulate().
from itertools import accumulate
s = 'mississippi'
print(tuple(accumulate(sorted(s)))[-1])
sorted(s) -> [‘i’, ‘i’, ‘i’, ‘i’, ‘m’, ‘p’, ‘p’, ‘s’, ‘s’, ‘s’, ‘s’]
tuple(accumulate(sorted(s)) -> (‘i’, ‘ii’, ‘iii’, ‘iiii’, ‘iiiim’, ‘iiiimp’, ‘iiiimpp’, ‘iiiimpps’, ‘iiiimppss’, ‘iiiimppsss’, ‘iiiimppssss’)
We are selecting the last index (-1) of the tuple