在 Python 中对嵌套列表进行排序和分组 [英] Sorting and Grouping Nested Lists in Python

查看:111
本文介绍了在 Python 中对嵌套列表进行排序和分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据结构(列表列表)

<预><代码>[['4','21','1','14','2008-10-24 15:42:58'],['3', '22', '4', '2somename', '2008-10-24 15:22:03'],['5','21','3','19','2008-10-24 15:45:45'],['6', '21', '1', '1somename', '2008-10-24 15:45:49'],['7', '22', '3', '2somename', '2008-10-24 15:45:51']]

我希望能够

  1. 使用函数对列表重新排序,以便我可以按列表中的每个项目进行分组.例如,我希望能够按第二列进行分组(以便所有 21 个都在一起)

  2. 使用函数仅显示每个内部列表中的某些值.例如,我想将此列表减少为仅包含2somename"的第 4 个字段值

所以列表看起来像这样

<预><代码>[['3', '22', '4', '2somename', '2008-10-24 15:22:03'],['7', '22', '3', '2somename', '2008-10-24 15:45:51']]

解决方案

对于第一个问题,您应该做的第一件事是使用 itemgetter 来自操作员模块:

x = [['4','21','1','14','2008-10-24 15:42:58'],['3', '22', '4', '2somename', '2008-10-24 15:22:03'],['5','21','3','19','2008-10-24 15:45:45'],['6', '21', '1', '1somename', '2008-10-24 15:45:49'],['7', '22', '3', '2somename', '2008-10-24 15:45:51']]from 操作符导入 itemgetterx.sort(key=itemgetter(1))

然后就可以使用 itertools 的 groupby 函数:

from itertools import groupbyy = groupby(x, itemgetter(1))

现在 y 是一个包含 (element, item iterator) 元组的迭代器.解释这些元组比展示代码更令人困惑:

对于elt,groupby(x, itemgetter(1))中的项目:打印(elt,项目)对于 i 在项目中:打印(一)

打印:

21 ['4'、'21'、'1'、'14'、'2008-10-24 15:42:58']['5'、'21'、'3'、'19'、'2008-10-24 15:45:45']['6', '21', '1', '1somename', '2008-10-24 15:45:49']22 ['3', '22', '4', '2somename', '2008-10-24 15:22:03']['7', '22', '3', '2somename', '2008-10-24 15:45:51']

对于第二部分,您应该使用这里已经提到的列表推导式:

from pprint import pprint as pppp([y for y in x if y[3] == '2somename'])

打印:

[['3', '22', '4', '2somename', '2008-10-24 15:22:03'],['7'、'22'、'3'、'2somename'、'2008-10-24 15:45:51']]

I have the following data structure (a list of lists)

[
 ['4', '21', '1', '14', '2008-10-24 15:42:58'], 
 ['3', '22', '4', '2somename', '2008-10-24 15:22:03'], 
 ['5', '21', '3', '19', '2008-10-24 15:45:45'], 
 ['6', '21', '1', '1somename', '2008-10-24 15:45:49'], 
 ['7', '22', '3', '2somename', '2008-10-24 15:45:51']
]

I would like to be able to

  1. Use a function to reorder the list so that I can group by each item in the list. For example I'd like to be able to group by the second column (so that all the 21's are together)

  2. Use a function to only display certain values from each inner list. For example i'd like to reduce this list to only contain the 4th field value of '2somename'

so the list would look like this

[
     ['3', '22', '4', '2somename', '2008-10-24 15:22:03'], 
     ['7', '22', '3', '2somename', '2008-10-24 15:45:51']
]

解决方案

For the first question, the first thing you should do is sort the list by the second field using itemgetter from the operator module:

x = [
 ['4', '21', '1', '14', '2008-10-24 15:42:58'], 
 ['3', '22', '4', '2somename', '2008-10-24 15:22:03'], 
 ['5', '21', '3', '19', '2008-10-24 15:45:45'], 
 ['6', '21', '1', '1somename', '2008-10-24 15:45:49'], 
 ['7', '22', '3', '2somename', '2008-10-24 15:45:51']
]

from operator import itemgetter

x.sort(key=itemgetter(1))

Then you can use itertools' groupby function:

from itertools import groupby
y = groupby(x, itemgetter(1))

Now y is an iterator containing tuples of (element, item iterator). It's more confusing to explain these tuples than it is to show code:

for elt, items in groupby(x, itemgetter(1)):
    print(elt, items)
    for i in items:
        print(i)

Which prints:

21 <itertools._grouper object at 0x511a0>
['4', '21', '1', '14', '2008-10-24 15:42:58']
['5', '21', '3', '19', '2008-10-24 15:45:45']
['6', '21', '1', '1somename', '2008-10-24 15:45:49']
22 <itertools._grouper object at 0x51170>
['3', '22', '4', '2somename', '2008-10-24 15:22:03']
['7', '22', '3', '2somename', '2008-10-24 15:45:51']

For the second part, you should use list comprehensions as mentioned already here:

from pprint import pprint as pp
pp([y for y in x if y[3] == '2somename'])

Which prints:

[['3', '22', '4', '2somename', '2008-10-24 15:22:03'],
 ['7', '22', '3', '2somename', '2008-10-24 15:45:51']]

这篇关于在 Python 中对嵌套列表进行排序和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆