1

我正在迭代包含约 500.000 个子列表的 10-20 个长列表的集合。列表如下所示:

A = [['a', 'b', 1.7], ['d', 'e', 6.2] ...]
B = [['a', 'b', 2.0], ['d', 'e', 10.0] ...] 
C = [['a', 'b', 3.0], ['d', 'e',7.0] ...]

等等......我的目标是在最后获得一个列表,如下所示:

final = [['a', 'b', 1.7, 2.0, 3.0], ['d', 'e', 6.2, 6.2, 10.0, 7.0] ...]

我已经通过将模板列表(例如 A)与包含所有列表值(总计)的列表进行比较来使用嵌套循环:

total =[['a', 'b', 1.7], ['d', 'e', 6.2], ['a', 'b', 2.0], ['d', 'e', 10.0], ['a', 'b', 3.0], ['d', 'e',7.0]]

temp = []
for i in A:
    new = [i[0:1]]
    for j in total:
        if i[0] == j[0]:
           new.append(j[2])
    temp.append(new)

除了初始字符串包含在子列表中之外,我得到的东西接近我正在寻找的东西。但这在以后很容易解决。这种方法的问题是考虑到列表的大小,完整的过程需要大量的时间。任何缩短此过程的替代建议或提示将不胜感激。

4

1 回答 1

3

A dict would be more appropriate here as it'll allow you to access values related to any key in O(1) time.

Using collections.defaultdict:

>>> from collections import defaultdict
>>> total =[['a', 'b', 1.7], ['d', 'e', 6.2], ['a', 'b', 2.0], ['d', 'e', 10.0], ['a', 'b', 3.0], ['d', 'e',7.0]]
>>> dic = defaultdict(list)
>>> for item in total:
        key = tuple(item[:2])  #tuples can be used as dictionary keys
        val = item[2]
        dic[key].append(val)
...     
>>> dic
defaultdict(<type 'list'>,
{('a', 'b'): [1.7, 2.0, 3.0],
 ('d', 'e'): [6.2, 10.0, 7.0]})

Using normal dict:

>>> dic = {}
>>> for item in total:
        key = tuple(item[:2])  #tuples can be used as dictionary keys
        val = item[2]
        dic.setdefault(key, []).append(val)
...     
>>> dic
{('a', 'b'): [1.7, 2.0, 3.0], ('d', 'e'): [6.2, 10.0, 7.0]}
于 2013-08-06T19:58:40.843 回答