简体   繁体   English

Python:如何按字母顺序对字符串中的字母进行排序,以区分大写字母和小写字母

[英]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 如果我正确理解您的要求,您需要对字符串进行排序

  1. without changing the case of letters 不改变字母的情况
  2. as if all the letters have the same case 好像所有的字母都有相同的情况

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.

相关问题 如何在Python中按字母顺序对字符串中的字母进行排序 - How to sort the letters in a string alphabetically in Python 按字母顺序对每个字符串中的字母进行排序 Python function - Sort the letters in each string alphabetically Python function Python:如何对字符串中的字母进行排序以使某些字符保持原状? - Python: How to sort the letters of a string keeping some characters in place? python:如何按字母顺序对大写字母进行排序 - python: how to sort lists alphabetically with respect to capitalized letters python:如何按字母顺序和 len 对字符串进行排序 - python: how to sort a string alphabetically and by len 如何按字母顺序排序此python代码 - how to sort this python code alphabetically 如何通过将不同情况下的相同字母在python中视为相同字母来按字母顺序对列表进行排序 - How to sort a list alphabetically by treating same letters in different case as same in python 如何按字母顺序对字符串进行排序,小写字母在大写字母之前? - How to sort a string alphabetically, with lower case letters coming before their upper case equivalents? 如何编辑这个简单的 function 以按字母顺序正确排序列表中的字母和单词(Python)? - How to edit this simple function to correctly sort letters and words in list alphabetically (Python)? 如何使用冒泡排序 python 按字母顺序对单词进行排序 - how to sort the words alphabetically with bubble sort python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM