简体   繁体   English

尝试对字母数字坐标进行排序

[英]Trying to sort alphanumeric coordinates

I have a list of coordinates and I want to sort them by the numbers that are inbetween text and symbols.我有一个坐标列表,我想按文本和符号之间的数字对它们进行排序。

coords = ['`154blue', '`155blue', 'a154blue', 'a155blue', 'b154blue', 'b155blue', 'c154blue', 'c155blue', 'd154blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown', 'b150brown']

I would want it to be listed like this:我希望它像这样列出:

coords = ['b150brown','`154blue', 'a154blue', 'b154blue', 'c154blue','d154blue','`155blue', 'a155blue', 'b155blue', 'c155blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown']

I have tried different sorting and lambda sorting but I cant get it to do it by the numbers.我尝试过不同的排序和 lambda 排序,但我无法通过数字来做到这一点。

You can use a regular expression to obtain the number, and sort first on that then by the underlying string say if there are two coords with the same number:您可以使用正则表达式来获取数字,然后首先对其进行排序,然后根据底层字符串进行排序,说明是否有两个具有相同数字的坐标:

import re

coords = ['`154blue', '`155blue', 'a154blue', 'a155blue', 'b154blue', 'b155blue', 'c154blue', 'c155blue', 'd154blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown', 'b150brown']
coords.sort(key=lambda c: (re.search(r'\d+', c).group(0), c))
print(coords)

Output:输出:

['b150brown', '`154blue', 'a154blue', 'b154blue', 'c154blue', 'd154blue', '`155blue', 'a155blue', 'b155blue', 'c155blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown']

You can use c in '0123456789' or c.isdigit to filter out characters which are not digits:您可以c in '0123456789'c.isdigit使用c in '0123456789'来过滤掉不是数字的字符:

>>> s = 'b150brown'
>>> ''.join(c for c in s if c.isdigit())
'150'
>>> int(''.join(c for c in s if c.isdigit()))
150

Then use this as the key argument to list.sort :然后将其用作list.sortkey参数:

coords = ['`154blue', '`155blue', 'a154blue', 'a155blue', 'b154blue', 'b155blue', 'c154blue', 'c155blue', 'd154blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown', 'b150brown']
coords.sort(key=lambda s: int(''.join(c for c in s if c.isdigit())))
print(coords)
# ['b150brown', '`154blue', 'a154blue', 'b154blue', 'c154blue', 'd154blue', '`155blue', 'a155blue', 'b155blue', 'c155blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown']

Additionally, you can break ties by sorting the strings lexicographically when they have the same number, as suggested by Shash Sinha :此外,如Shash Sinha所建议的,您可以通过按字典顺序对具有相同编号的字符串进行排序来打破平局

coords = ['`154blue', '`155blue', 'a154blue', 'a155blue', 'b154blue', 'b155blue', 'c154blue', 'c155blue', 'd154blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown', 'b150brown']
coords.sort(key=lambda s: (int(''.join(c for c in s if c.isdigit())), s))
print(coords)
# ['b150brown', '`154blue', 'a154blue', 'b154blue', 'c154blue', 'd154blue', '`155blue', 'a155blue', 'b155blue', 'c155blue', 'd155blue', 'e155blue', '`156brown', 'a156brown', 'a158brown']

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM