[英]Python: How to sort the letters in a string alphabetically keeping distinction between uppercases and lowercases
I am trying to order the words of a string in a particular way: In my code below the output is "MNWdeorwy" but i would like it to be "deMNorWwy" (so i need to keep the letters ordered despite being upper o lowercases) Could you please help me to understand where I am wrong and why? 我试图以特定的方式命令字符串的单词:在我的代码下面输出是“MNWdeorwy”但我希望它是“deMNorWwy” (所以我需要保持字母顺序,尽管是上o小写)你能帮我理解我错在哪里,为什么? Thank you
谢谢
wrd = "MyNewWord"
def order_word(s):
if s == "":
return "Invalid String!"
else:
c = sorted(s)
d = ''.join(sorted(c))
return d
print order_word(wrd)
I would like to precise that my question is different from the following: How to sort the letters in a string alphabetically in Python : in fact, the answers given in the link does not consider the difference between upper and lowercases in a string. 我想确切地说我的问题与以下内容不同: 如何在Python中按字母顺序对字符串中的字母进行排序 :实际上,链接中给出的答案不考虑字符串中大写字母和小写字母之间的区别。
sorted()
sorts based off of the ordinal of each character. sorted()
根据每个字符的序数进行排序。 Capital letters have ordinals that are lower than all lowercase letters. 大写字母的序数低于所有小写字母。 If you want different behavior, you'll need to define your own key:
如果您想要不同的行为,则需要定义自己的密钥:
c = sorted(s, key=lambda c: (c.lower(), c.islower()))
That way, c
would be sorted by ('c', 1)
and C
is sorted by ('c', 0)
. 这样,
c
将按('c', 1)
排序, C
按('c', 0)
排序。 Both come before ('d', ...)
or ('e', ...)
etc., but the capital C
is earlier (lower) than the lowercase c
. 两者都来自
('d', ...)
或('e', ...)
等,但是大写C
比小写c
更早(更低)。
By the way, you shouldn't say d = "".join(sorted(c))
because c
has already been sorted. 顺便说一句,你不应该说
d = "".join(sorted(c))
因为c
已经被排序了。 Just do d = "".join(c)
只做
d = "".join(c)
If I understand correctly your requirements, you want to sort a string 如果我正确理解您的要求,您需要对字符串进行排序
this can be achieved, eg, 这可以实现,例如,
In [44]: a = 'zWea'
In [45]: sorted(a,key=lambda c:c.upper())
Out[45]: ['a', 'e', 'W', 'z']
In [46]:
that works because you transform momentarily individual characters during a comparison. 这是有效的,因为你在比较过程中瞬间转换个别角色。
Forgot to mention, you can mix non-alphabetical chars in your string, but a few characters are placed between upper and lower case alphabetical chars (eg, the ^
caret), so what you get depends on using .lower()
or .upper()
method of strings, 忘了提一下,你可以在你的字符串中混合非字母字符,但是在大写和小写字母字符之间放置一些字符(例如,
^
插入符号),所以你得到的取决于使用.lower()
或.upper()
字符串方法,
In [56]: sorted('abCD^',key=lambda c:c.lower())
Out[56]: ['^', 'a', 'b', 'C', 'D']
In [57]: sorted('abCD^',key=lambda c:c.upper())
Out[57]: ['a', 'b', 'C', 'D', '^']
In [58]:
You can also try like this 你也可以这样试试
import re
def natural_sort(wrd):
convert = lambda text: int(text) if text.isdigit() else text.lower()
final = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return ''.join(sorted(wrd, key = final))
Output: 输出:
>>> natural_sort(wrd)
'deMNorwWy'
OR 要么
You can do with third party library for this on PyPI called natsort 您可以在名为natsort的PyPI上使用第三方库
https://pypi.python.org/pypi/natsort https://pypi.python.org/pypi/natsort
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.