Python:嵌套列表修改 [英] Python: Nested List Modification

查看:291
本文介绍了Python:嵌套列表修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配对数据的嵌套列表,格式为:

I have a nested list of paired data in the format:

mylist = [['item1', 'some other stuff', 'value1'],['item1', 'some other stuff', 'value2'],['item2', 'some other stuff', 'value3'],['item2', 'some other stuff', 'value4']]

我不知道如何执行以下操作,但是我需要:

I have no idea how to do the following, but I need to:

我需要按照以下方式对列表进行分组:

I need the list to be grouped as such:

[['item1', 'value1', 'value2'], ['item2', 'value3', 'value4']]

因此,对于我的项目列表,如果该项目在列表中以不同的值重复多次,则所有值都应与其对应的项目分组.

So for my list of items, all of the values should be grouped with their corresponding item if the item is repeated multiple times in the list with different values.

任何帮助将不胜感激.

谢谢

推荐答案

让我们从使用字典开始,将项映射到值列表.这将比列表更容易(和更快),因为找出要向其添加新值的列表只是mydict[item],而不必编写某种线性搜索函数.

Let's start out by using a dictionary, to map items to lists of values. That's going to be a lot easier (and faster) than a list, because to figure out which list to add the new value to is just mydict[item] instead of having to write some kind of linear-search function.

mydict = {}
for item, otherstuff, value in mylist:
    mydict.setdefault(item, []).append(value)

这给您:

{'item1': ['value1', 'value2'], 'item2': ['value3', 'value4']}

现在,如果您愿意,我们可以将该词典转换回列表:

Now we can convert that dictionary back to a list, if you want:

groupedlist = [[k] + v for k, v in mydict.items()]

这给您:

[['item2', 'value3', 'value4'], ['item1', 'value1', 'value2']]


这里最大的缺点是,一旦将事情写成字典,就会失去任何原始命令.如果您期望item1首先出现是因为它的第一个条目在item2的第一个条目之前(或者因为item2的最后一个条目在item1的后面??),那么您就已经失去了它.如果重要的话,可以使用OrderedDict.


The big downside here is that once you stick things into a dict, you lose any original order. If you were expecting item1 to come first because its first entry came before item2's first entry (or because item2's last entry came after item1's maybe?), you've lost that. If it's important, you can use an OrderedDict.

最大的好处是,实际上,您实际上想要的是字典,而不是列表.

The big upside is that often, you actually want a dictionary in the end, not a list.

较小的好处是,如果不对数据进行排序,则groupby(…sorted(…))需要O(NlogN)排序,而此解决方案是O(N).通常,那不会有什么不同.如果这样做的话,给定Python实现和平台的常数因子差异可能总会超过差异.但是,如果性能很重要,请同时测试两个解决方案并使用速度更快的解决方案.

The smaller upside is that, if your data aren't sorted, groupby(…sorted(…)) requires an O(NlogN) sort, while this solution is O(N). Usually, that won't make a difference. And if it does, the constant-factor differences for a given Python implementation and platform might outweigh the differences anyway. But if the performance matters, test both solutions and use the faster one.

这篇关于Python:嵌套列表修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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