Python:如何根据对象的特征或属性对对象列表进行分组? [英] Python: How to group a list of objects by their characteristics or attributes?

查看:645
本文介绍了Python:如何根据对象的特征或属性对对象列表进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将对象列表分成子列表,其中具有相同属性/特性的对象保留在同一子列表中。

I want to separate a list of objects into sublists, where objects with same attribute/characteristic stay in the same sublist.

假设我们有一个字符串列表:

Suppose we have a list of strings:

["This", "is", "a", "sentence", "of", "seven", "words"]

我们要根据字符串的长度将字符串分开,如下所示:

We want to separate the strings based on their length as follows:

[['sentence'], ['a'], ['is', 'of'], ['This'], ['seven', 'words']]

我目前想出的程序是这个

The program I currently come up with is this

sentence = ["This", "is", "a", "sentence", "of", "seven", "words"]
word_len_dict = {}
for word in sentence:
    if len(word) not in word_len_dict.keys():
        word_len_dict[len(word)] = [word]
    else:
        word_len_dict[len(word)].append(word)


print word_len_dict.values()

我想知道是否有更好的方法来实现这一目标?

I want to know if there is a better way to achieve this?

推荐答案

看看 itertools.groupby() 。请注意,您的列表必须首先排序(比方法OP 昂贵)。

>>> from itertools import groupby
>>> l = ["This", "is", "a", "sentence", "of", "seven", "words"]
>>> print [list(g[1]) for g in groupby(sorted(l, key=len), len)]
[['a'], ['is', 'of'], ['This'], ['seven', 'words'], ['sentence']]

您想要字典->

>>> {k:list(g) for k, g in groupby(sorted(l, key=len), len)}
{8: ['sentence'], 1: ['a'], 2: ['is', 'of'], 4: ['This'], 5: ['seven', 'words']}

这篇关于Python:如何根据对象的特征或属性对对象列表进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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