简体   繁体   English

如何在python中对字符串进行排序?

[英]How can I sort a string of characters in python?

I cannot find an answer elsewhere on this site, which is why I am asking.我在本网站的其他地方找不到答案,这就是我问的原因。 My answer is different as it contains alpha-numeric values and will not work with any integer based solution here is a sample of my string:我的答案是不同的,因为它包含字母数字值并且不适用于任何基于整数的解决方案,这里是我的字符串示例:

01768
01785
01799
01804
01818
01821
01835
01849
01852
01866
XXXXX

What can i do to sort these in ascending/descending order, so far i have tried sorted() as it is used for stings, the output of this is ['0', '1', '6', '6', '8'] ['X', 'X', 'X', 'X', 'X'] .我能做些什么来按升序/降序对它们进行排序,到目前为止我已经尝试过sorted()因为它用于刺痛,它的输出是['0', '1', '6', '6', '8'] ['X', 'X', 'X', 'X', 'X'] i want each separate value to be ordered and not individual characters.我希望对每个单独的值进行排序,而不是单个字符。 Any help is appreciated.任何帮助表示赞赏。

If you want to sort all these strings by their integer value then you can do this :如果要按整数值对所有这些字符串进行排序,则可以执行以下操作:

sorted(arr,key=lambda x:int(x))

Where arr is the list of all those strings.其中arr是所有这些字符串的列表。

Using sorted使用sorted

Ex:前任:

s = """01768
01785
01799
01804
01818
01821
01835
01849
01852
01866"""

print("Ascending")
print( "\n".join(map(str, sorted(map(int, s.splitlines())))) )
print("---")
print("Descending")
print( "\n".join(map(str, sorted(map(int, s.splitlines()), reverse=True))) )

Output:输出:

Ascending
1768
1785
1799
1804
1818
1821
1835
1849
1852
1866
---
Descending
1866
1852
1849
1835
1821
1818
1804
1799
1785
1768

Using this answer as a guide Does Python have a built in function for string natural sort?使用此答案作为指南Python 是否具有用于字符串自然排序的内置函数?

alist = ['01768',
'01785',
'01799',
'01804',
'01818',
'01821',
'01835',
'01849',
'01852',
'01866',
'XXXXX']

from natsort import natsorted, ns
r = natsorted(alist, key=lambda y: y.lower())

>>>print(r)
['01768', '01785', '01799', '01804', '01818', '01821', '01835', '01849', '01852', '01866', 'XXXXX']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何排序字符串中的日期? (蟒蛇) - How can I sort dates that are in String? (python) 在Python中,我如何自然地对字母数字字符串列表进行排序,以使字母字符排在数字字符之前? - In Python, how can I naturally sort a list of alphanumeric strings such that alpha characters sort ahead of numeric characters? 如何在Python中替换字符串中的字符,但不使用字符串方法? - How can I replace characters in a string in Python, but not using string methods? 如何在 python 中按字母顺序对字符串进行排序 - How can i sort a string in alphabetical order in python 如何在Python字符串中写入原始字节字符? - How can I write raw byte characters in Python string? 如何更改python中字符串的前5个字符? - How can I change the first 5 characters of a string in python? "<i>How can I check if a string has the same characters?<\/i>如何检查字符串是否具有相同的字符?<\/b> <i>Python<\/i> Python<\/b>" - How can I check if a string has the same characters? Python 如何在python中分隔字符串中的字符? - How can I separate characters inside a string in python? 如何从字符串中删除前 2 个字符 - python - How can I strip the first 2 characters from a string - python (Python)如何将字符添加到集合中的某个字符串? - (Python) How can I add characters to a certain string in a set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM