我如何仅通过使用reduce函数在python中创建单词计数输出? [英] how can I create word count output in python just by using reduce function?

查看:63
本文介绍了我如何仅通过使用reduce函数在python中创建单词计数输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下元组列表: [('a',1),('a',1),('b',1),('c',1),('a',1),('c',1)]

我想知道是否可以利用python的 reduce 函数聚合它们并产生以下输出: [('a',3),('b',1),('c',2)]

I would like to know if I can utilize python's reduce function to aggregate them and produce the following output : [('a', 3), ('b', 1), ('c', 2)]

或者如果还有其他方法,我也想知道(循环很好)

Or if there are other ways, I would like to know as well (loop is fine)

推荐答案

使用 reduce 似乎很难实现,因为如果您"reduce"的两个元组都没有相同的字母,那么您无法计算结果.如何将('a',1)('b',1)减少到可行的结果?

It seems difficult to achieve using reduce, because if both tuples that you "reduce" don't bear the same letter, you cannot compute the result. How to reduce ('a',1) and ('b',1) to some viable result?

我最好能做的是 l = functools.reduce(lambda x,y:(x [0],x [1] + y [1])如果x [0] == y [0]否则x + y,sorted(l))

我得到了 ('a', 3, 'b', 1, 'c', 1, 'c', 1).因此,它对于第一个元素来说是可行的,但需要多次通过才能进行其他操作(重新创建元组并进行另一个类似的 reduce ,至少可以说效率不高!).

it got me ('a', 3, 'b', 1, 'c', 1, 'c', 1). So it kind of worked for the first element, but would need more than one pass to do the other ones (recreating tuples and make another similar reduce, well, not very efficient to say the least!).

无论如何,这是两种可行的工作方式

Anyway, here are 2 working ways of doing it

首先,使用 collections.Counter 计数相同种类的元素:

First, using collections.Counter counting elements of the same kind:

l = [('a', 1), ('a', 1), ('b', 1), ('c',1), ('a', 1), ('c', 1)]

import collections

c = collections.Counter()
for a,i in l:
    c[a] += i

我们不能使用listcomp,因为每个元素都有权重(即使这里是1)

We cannot use listcomp because each element has a weight (even if here it is 1)

结果:一个字典: Counter({'a':3,'c':2,'b':1})

第二个选项:在排序列表上使用 itertools.groupby ,按名称/字母分组,并对带有相同字母的整数求和:

Second option: use itertools.groupby on the sorted list, grouping by name/letter, and performing the sum on the integers bearing the same letter:

print ([(k,sum(e for _,e in v)) for k,v in itertools.groupby(sorted(l),key=lambda x : x[0])])

结果:

[('a', 3), ('b', 1), ('c', 2)]

这篇关于我如何仅通过使用reduce函数在python中创建单词计数输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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