简体   繁体   English

自然地通过元组中Python的第一个元素对字母数字元组进行排序

[英]Naturally sort a list of alpha-numeric tuples by the tuple's first element in Python

A previous stackoverflow question explains how to sort a list of strings alpha-numerically. 之前的stackoverflow问题解释了如何按字母顺序对字符串列表进行排序。 I would like to sort a list of tuples alphanumerically by the tuple's first element. 我想用元组的第一个元素按字母数字排序元组列表。

Example 1: 例1:

>>> sort_naturally_tuple([('b', 0), ('0', 1), ('a', 2)])
[('0', 1), ('a', 2), ('b', 0)]

Example 2: 例2:

>>> sort_naturally_tuple([('b10', 0), ('0', 1), ('b9', 2)])
[('0', 1), ('b9', 2), ('b10', 0)]

Update: To emphasize the alphanumeric factor, please review example 2. 更新:要强调字母数字因素,请查看示例2。

Using the second answer from the other question, generalized to support any method on item as the basis for getting the key: 使用另一个问题的第二个答案,概括为支持项目上的任何方法作为获取密钥的基础:

import re
from operator import itemgetter

def sorted_nicely(l, key):
    """ Sort the given iterable in the way that humans expect."""
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda item: [ convert(c) for c in re.split('([0-9]+)', key(item)) ]
    return sorted(l, key = alphanum_key)


print sorted_nicely([('b10', 0), ('0', 1), ('b9', 2)], itemgetter(0))

This is exactly the same as that answer except generalized to use any callable as the operation on item. 这与该答案完全相同,除了通用以使用任何可调用项作为对项的操作。 If you just wanted to do it on a string, you'd use lambda item: item , if you wanted to do it on a list, tuple, dict, or set, you'd use operator.itemgetter(key_or_index_you_want) , or if you wanted to do it on a class instance you could use operator.attrgetter('attribute_name_you_want') . 如果你只是想在一个字符串上做,你可以使用lambda item: item ,如果你想在列表,元组,字典或集合上做,你可以使用operator.itemgetter(key_or_index_you_want) ,或者如果你想使用它你想在类实例上做到这一点你可以使用operator.attrgetter('attribute_name_you_want')

It gives 它给

[('0', 1), ('b9', 2), ('b10', 0)]

for your example #2. 为你的例子#2。

Tuples are by default sorted by their elements, starting at the first. 默认情况下,元组按其元素排序,从第一个开始。 So simply do 这么简单

L = [('b', 0), ('0', 1), ('a', 2)]
L.sort()
print L
# or create a new, sorted list
print sorted([('b', 0), ('0', 1), ('a', 2)])

The question you liked to talks about natural sorting, which is different from normal (alphanumeric) sorting. 您喜欢谈论自然排序的问题,这与普通(字母数字)排序不同。

Lets say you want to do natural sort on the first item only: 假设您只想对第一项进行自然排序:

import re
def naturalize(item):
    # turn 'b10' into ('b',10) which sorts correctly
    m = re.match(r'(\w+?)(\d+)', item)
    return m.groups()
# now sort by using this function on the first element of the tuple:
print sorted(L, key=lambda tup: naturalize(tup[0]))

As others have pointed out, sorted will use the first element of the tuple by default. 正如其他人所指出的,sorted将默认使用元组的第一个元素。 If you wish to modify this default behavior you can specify a key to be used during the comparisons. 如果要修改此默认行为,可以指定在比较期间使用的键。

sorted([('b', 0), ('0', 1), ('a', 2)])

Will return the same as: 将返回相同的:

sorted([('b', 0), ('0', 1), ('a', 2)], key=lambda item: item[0])

To sort by the second element however try: 要按第二个元素排序,请尝试:

sorted([('b', 0), ('0', 1), ('a', 2)], key=lambda item: item[1])

The natsort module does this by default without any extra work natsort模块默认执行此操作,无需任何额外工作

>>> from natsort import natsorted
>>> natsorted([('b', 0), ('0', 1), ('a', 2)])
[('0', 1), ('a', 2), ('b', 0)]
>>> natsorted([('b10', 0), ('0', 1), ('b9', 2)])
[('0', 1), ('b9', 2), ('b10', 0)]

暂无
暂无

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

相关问题 如何使用字母数字字符从单个列表创建元组? - How to create tuples from a single list with alpha-numeric chacters? 在Python中,我如何自然地对字母数字字符串列表进行排序,以使字母字符排在数字字符之前? - In Python, how can I naturally sort a list of alphanumeric strings such that alpha characters sort ahead of numeric characters? 按Python 3中的特定元组元素对元组列表进行排序 - Sort list of tuples by specific tuple element in Python 3 用python中元组列表中元组的第一个元素索引元素的最快方法 - Fastest way to index an element by the first element of a tuple in a list of tuples in python 在列表中访问元组列表中元组的第一个元素 - python - Accessing first element of a tuple in a list of tuples, in a list - python 组织字母数字字符串列表 - Organizing a list of alpha-numeric strings 按每个元组的第一个元素对元组列表进行排序-Python 2.7 - Sorting list of tuples by first element of each tuple - Python 2.7 Python元组列表中第二个和第三个元素的和,按第一个分组 - Python sum of second and third element of tuple in list of tuples grouped by first Python3:按元组第一个元素中包含的数据戳对元组列表进行排序 - Python3: Sorting a list of tuples by datastamp contained in first element of tuple 如何按第二个元组元素对元组列表进行排序? - How to sort a list of tuples, by the second tuple element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM