How to reduce on a list of tuples in python(如何减少python中的元组列表)
问题描述
我有一个数组,我想计算数组中每个项目的出现次数.
I have an array and I want to count the occurrence of each item in the array.
我已经设法使用 map 函数来生成一个元组列表.
I have managed to use a map function to produce a list of tuples.
def mapper(a):
return (a, 1)
r = list(map(lambda a: mapper(a), arr));
//output example:
//(11817685, 1), (2014036792, 1), (2014047115, 1), (11817685, 1)
我希望 reduce 函数可以帮助我按每个元组中的第一个数字 (id) 对计数进行分组.例如:
I'm expecting the reduce function can help me to group counts by the first number (id) in each tuple. For example:
(11817685, 2), (2014036792, 1), (2014047115, 1)
我试过了
cnt = reduce(lambda a, b: a + b, r);
还有其他一些方法,但它们都不起作用.
and some other ways but they all don't do the trick.
注意感谢所有关于解决问题的其他方法的建议,但我只是在学习 Python 以及如何在这里实现 map-reduce,并且我已经简化了我的实际业务问题以使其易于理解,所以请告诉我一个正确的方法来做 map-reduce.
NOTE Thanks for all the advice on other ways to solve the problems, but I'm just learning Python and how to implement a map-reduce here, and I have simplified my real business problem a lot to make it easy to understand, so please kindly show me a correct way of doing map-reduce.
推荐答案
你可以使用Counter
:
from collections import Counter
arr = [11817685, 2014036792, 2014047115, 11817685]
counter = Counter(arr)
print zip(counter.keys(), counter.values())
正如@ShadowRanger 所指出的, Counter
有 items()
方法:
As pointed by @ShadowRanger Counter
has items()
method:
from collections import Counter
arr = [11817685, 2014036792, 2014047115, 11817685]
print Counter(arr).items()
这篇关于如何减少python中的元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何减少python中的元组列表


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01