Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python3汇总元素表单列表_Python_List_Python 3.x_Sublist - Fatal编程技术网

python3汇总元素表单列表

python3汇总元素表单列表,python,list,python-3.x,sublist,Python,List,Python 3.x,Sublist,我有一份清单 lst = [[0,1],[1,1],[3,1],[1,2],[1,3],[3,5]] 我想将子列表的第二项汇总为相等的第一项。结果应该是这样的 lst = [[0,1],[1,6],[3,6]] 我试过这样的方法: lst=[[0, 0.25], [1, 0.125], [2, 0.0625], [3, 0.0625], [1, 0.125], [2, 0.0625], [3, 0.03125], [4, 0.03125]] for listitem in range(len

我有一份清单

lst = [[0,1],[1,1],[3,1],[1,2],[1,3],[3,5]]
我想将子列表的第二项汇总为相等的第一项。结果应该是这样的

lst = [[0,1],[1,6],[3,6]]
我试过这样的方法:

lst=[[0, 0.25], [1, 0.125], [2, 0.0625], [3, 0.0625], [1, 0.125], [2, 0.0625], [3, 0.03125], [4, 0.03125]]
for listitem in range(len(lst)):
    first=listitem[0]
    second = 0
    for obj in lst:
        if obj[0] == first:
            second += obj[1]
            lst.remove(obj)
    listitem[1] = second

考虑这一点的另一种方式是,每对中的第一项是
,第二项是
——因此,如果您创建一个
dict
,您可以在遇到每个键时将这些值相加——如果使用
defaultdict
,则更容易:

from collections import defaultdict

summary = defaultdict(int)
for sublist in lst:
    key, value = sublist
    summary[key] += value

print(summary.items())
这一行
summary[key]+=value
是这里的主力。它的作用是:

  • 在摘要中查找
  • 如果它不存在,添加它并使用
    int
    创建默认值
  • 返回值(已存在或新创建)
  • 向其添加新值
  • 将其存储回
    键下的
    摘要

考虑这一点的另一种方式是,每对中的第一项是
键,第二项是
值-
——因此,如果您创建一个
dict
,您可以在遇到每个键时将这些值加在一起,如果使用
defaultdict
,就更容易了:

from collections import defaultdict

summary = defaultdict(int)
for sublist in lst:
    key, value = sublist
    summary[key] += value

print(summary.items())
这一行
summary[key]+=value
是这里的主力。它的作用是:

  • 在摘要中查找
  • 如果它不存在,添加它并使用
    int
    创建默认值
  • 返回值(已存在或新创建)
  • 向其添加新值
  • 将其存储回
    键下的
    摘要
使用defaultdict

    from collections import defaultdict

    lst=[[0, 0.25], [1, 0.125], [2, 0.0625], [3, 0.0625], [1, 0.125], [2, 0.0625], [3, 0.03125], [4, 0.03125]]

    res = defaultdict(int)    
    for el in lst:
        res[el[0]] += el[1] 
    res_list = [[k,v] for (k,v) in res.items()]

    print(res_list)

[[0, 0.25], [1, 0.25], [2, 0.125], [3, 0.09375], [4, 0.03125]]
使用defaultdict

    from collections import defaultdict

    lst=[[0, 0.25], [1, 0.125], [2, 0.0625], [3, 0.0625], [1, 0.125], [2, 0.0625], [3, 0.03125], [4, 0.03125]]

    res = defaultdict(int)    
    for el in lst:
        res[el[0]] += el[1] 
    res_list = [[k,v] for (k,v) in res.items()]

    print(res_list)

[[0, 0.25], [1, 0.25], [2, 0.125], [3, 0.09375], [4, 0.03125]]

groupby
返回一个键,并对具有相同键的项执行迭代器。必须向其传递一个按相同键排序的列表:

from itertools import groupby
from operator import itemgetter

lst = [[0,1],[1,1],[3,1],[1,2],[1,3],[3,5]]

result = [[k,sum(b for a,b in g)]
           for k,g in groupby(sorted(lst),key=itemgetter(0))]

print(result)
输出:

 [[0, 1], [1, 6], [3, 6]]

groupby
返回一个键,并对具有相同键的项执行迭代器。必须向其传递一个按相同键排序的列表:

from itertools import groupby
from operator import itemgetter

lst = [[0,1],[1,1],[3,1],[1,2],[1,3],[3,5]]

result = [[k,sum(b for a,b in g)]
           for k,g in groupby(sorted(lst),key=itemgetter(0))]

print(result)
输出:

 [[0, 1], [1, 6], [3, 6]]
range(n)
生成一个列表,如
[0,1,2…n-1]
,因此
列表项的类型是
int
而不是
列表

lst=[[0, 0.25], [1, 0.125], [2, 0.0625], [3, 0.0625], [1, 0.125], [2, 0.0625], [3, 0.03125], [4, 0.03125]]
for listitem in lst:
    first = listitem[0]
    second = 0
    for obj in lst:
        if obj[0] == first:
            second += obj[1]
            lst.remove(obj)
    listitem[1] = second        
range(n)
生成一个列表,如
[0,1,2…n-1]
,因此
列表项的类型是
int
而不是
列表

lst=[[0, 0.25], [1, 0.125], [2, 0.0625], [3, 0.0625], [1, 0.125], [2, 0.0625], [3, 0.03125], [4, 0.03125]]
for listitem in lst:
    first = listitem[0]
    second = 0
    for obj in lst:
        if obj[0] == first:
            second += obj[1]
            lst.remove(obj)
    listitem[1] = second