简体   繁体   English

如何通过前两个值对python列表/元组的第三个值进行分组/汇总/减少(不使用for循环)?

[英]How to group/aggregate/reduce third values of a python list/tuple by the first two values (without using for loops)?

What is the best way to group/aggregate/reduce python tuples in the following manner (without using nested for loops). 以以下方式(不使用嵌套的for循环)对python元组进行分组/聚合/减少的最佳方法是什么?

For example, given the following tuples: 例如,给定以下元组:

entry1 = ('name1', 'surname1', 'product1')
entry2 = ('name1', 'surname1', 'product2')
entry3 = ('name1', 'surname1', 'product3')
entry4 = ('name2', 'surname2', 'product1')
entry5 = ('name2', 'surname2', 'product2')
entry6 = ('name2', 'surname2', 'product3')

How can the function func : 函数func如何:

func(entry1, entry2, entry3, entry4, entry5, entry6)

return the data in a similar format to: 以类似的格式返回数据:

(('name1', 'surname1', ('product1', 'product2', 'product3')),
('name2', 'surname2', ('product1', 'product2', 'product3')))

Notes: 笔记:

  • The order of results in not important. 结果的顺序并不重要。
  • The results can be a list or a tuple 结果可以是列表或元组
  • Enforcing product uniqueness is fine however not required. 强制执行产品唯一性很好,但是不是必需的。 That is results such as ('name1', 'surname1', ('product1', 'product1', 'product2', 'product3')) are fine. 那是很好的结果,例如('name1', 'surname1', ('product1', 'product1', 'product2', 'product3'))

You can use collections.defaultdict to group first and later convert to a list or tuple: 您可以使用collections.defaultdict进行分组,然后再转换为列表或元组:

from collections import defaultdict

def func(*args):
    d = defaultdict(list)
    for entry in args:
        d[entry[0], entry[1]].append(entry[2])
    return tuple((k[0], k[1], tuple(v)) for k, v in d.items())

Test: 测试:

entry1 = ('name1', 'surname1', 'product1')
entry2 = ('name1', 'surname1', 'product2')
entry3 = ('name1', 'surname1', 'product3')
entry4 = ('name2', 'surname2', 'product1')
entry5 = ('name2', 'surname2', 'product2')
entry6 = ('name2', 'surname2', 'product3')
print(func(entry1, entry2, entry3, entry4, entry5, entry6))

Result: 结果:

(('name1', 'surname1', ('product1', 'product2', 'product3')),
 ('name2', 'surname2', ('product1', 'product2', 'product3')))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对两个值上的元组列表进行分组,并返回所有第三个值的列表 - Group a list of tuples on two values, and return a list of all the third value Python 字典聚合值列表 - Python list of dictionaries aggregate values 如何在不使用集合的情况下在 Python 的两个字典值列表中找到公共元素? - How to find the common elements inside two list of values of dictionary in Python without using sets? Python元组列表中第二个和第三个元素的和,按第一个分组 - Python sum of second and third element of tuple in list of tuples grouped by first 如何在不丢失字符串值类型的情况下将字符串转换为列表/元组? - How to convert a string into a list/tuple without losing the type of the values in the string? 使用Python按列表中的唯一列值分组 - group by unique column values in list using Python 在 python 中使用列表理解优化两个 for 循环(没有 pandas) - Optimising two for loops using list comprehension in python(without pandas) 如何在不使用循环的情况下基于分配给它的先前值递归修改列表? - How to modify a list recursively based on previous values assigned to it without using loops? 如何在python 3.4.4中将dict值列表转换为元组? - How to convert list of dict values to tuple in python 3.4.4? 如何在不知道 Python 中哪个更大的情况下获取两个值之间的值列表? - How to get a list of values between two values without knowing which is greater in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM