分配价值的列表python和总结的价值

0

的问题

这是一个运动在python,我不能使用的大熊猫只是建立在蟒蛇的功能。 我有这个名单:

 ['a', 1],
 ['b', 1],
 ['a', 2],
 ['a', 2],

输出,应该是这样的:

 ['a', 5],
 ['b', 1],

这是码我有那么远,但我基本上相同的列表。

result = []

for x,y in list_data:
    if x not in result: 
        result.append([x, int(y)])
    else:
        result[1] += int(y) 
result
built-in function list python
2021-11-24 02:37:03
4

最好的答案

1

你可以做一个词典的计算的总数,然后将其转换回一名单:

data = [['c', 2], ['a', 1], ['b', 1], ['a', 2], ['a', 2], ['c', 3]]

totals = {} # could use collections.defaultdict for this part
for k, v in data:
    totals[k] = totals.get(k, 0) + v
print(totals) # {'c': 5, 'a': 5, 'b': 1}
output = [[k, v] for k, v in totals.items()]
# or, output = list(map(list, totals.items()))
# or, output = list(totals.items()) # if you are fine with a list of tuples
print(output) # [['c', 5], ['a', 5], ['b', 1]]

# if you want the output to be sorted alphabetically
output = sorted(output, key=lambda lst: lst[0])
print(output) # [['a', 5], ['b', 1], ['c', 5]]
2021-11-24 02:50:31

它的工作,你可以给我建议如何排序的项目按字母顺序
Luis Alejandro Vargas Ramos

@LuisAlejandroVargasRamos是不是已经排序的?
j1-lee

@LuisAlejandroVargasRamos太好了,我已经增加排序的一部分末尾的代码,用新的项目涉及 c 在输入。
j1-lee
1

一个班轮:

import itertools

[[x[0], sum([value[1] for value in x[1]])] for x in itertools.groupby(sorted(list_data), lambda i: i[0])]

详细信息:

[[x[0], sum([value[1] for value in x[1]])] for x in itertools.groupby(sorted(list_data), lambda i: i[0])]
                                                                      sorted(list_data)                    # sort the list alphabetically
                                                    itertools.groupby(sorted(list_data), lambda i: i[0])   # group by the "key"
                                                                                                           # result: [('a', <itertools._grouper object at 0x000001D6B806F700>), ('b', <itertools._grouper object at 0x000001D6B806F310>)]
  x[0]                                     for x in                                                        # ['a', 'b']
            [value[1] for value in x[1]]   for x in                                                        # [[1, 2, 2], [1]]
        sum([value[1] for value in x[1]])  for x in                                                        # [5, 1]
  x[0], sum([value[1] for value in x[1]])  for x in                                                        # [['a', 5], ['b', 1]]
2021-11-24 02:43:18
0

词典是最佳做法对于你的任务。

data = [['a', 1], ['b', 1], ['a', 2], ['a', 2]]
d = {}
for key, value in data:
    d[key] = value + (d[key] if key in d else 0)
# generate list of tuples and sort
out = sorted(d.items())
print(out)
2021-11-24 02:50:46
0

看来你是想列出了作为一个字典。 用你的代码的工作,你需要找到结果的指数,想要加入的价值(结果[1]).

但是,如果你改变的结果字典你可以保持几乎相同的代码:

result = dict() # or result = {}
for x,y in list_data:
    if x not in result: 
        result[x] = int(y)
    else:
        result[x] += y

print(result)

Out[1]: {'a': 5, 'b': 1}

如果你想有一个清单,可以使用:

result_list = []
for x,y in result.items():
    result_list.append([x,y])
    
print(result_list)
[['a', 5], ['b', 1]]
2021-11-24 02:56:01

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................