[英]does python have a partition_by function?
is there some established recipe for partitioning a list according to some predicate? 是否有一些根据某些谓词划分列表的既定方法? What I want to do is like an itertools groupby, but the predicate is going to be some arbitrarily complicated function (and not just a key).
我想要做的就像一个itertools groupby,但谓词将是一些任意复杂的函数(而不仅仅是一个键)。 For example, imagine a list of students, and each student has a list of courses, and I want to group by those with courses in common.
例如,想象一个学生列表,每个学生都有一个课程列表,我想由那些有共同课程的学生分组。 So it would be something like:
所以它会是这样的:
def coursework_intersection(a,b):
return set(a['courses']).intersection(b['courses'])
list_of_lists = partition_by(coursework_intersection, students)
If you want what Joran says in comments then it's inherently an Omega(n^2) worst case running time because that's the size of the output in the worst case (where coursework_intersection
always returns true). 如果你想要Joran在评论中所说的那么它本身就是Omega(n ^ 2)最坏情况下的运行时间,因为这是最坏情况下输出的大小(其中
coursework_intersection
总是返回true)。 So let's just bite the bullet: 所以,让我们咬紧牙关:
def associated_with(func, seq):
for item in seq:
yield item, (other for other in seq if func(item, other))
Note that the input is a sequence, not an iterable, since this is a multi-pass algorithm. 请注意,输入是序列,而不是可迭代的,因为这是一个多遍算法。
That could be optimized to call func
half as many times, if we're allowed to assume that it's a symmetric function, although the cost is more memory use. 如果我们被允许假设它是一个对称函数,那么可以优化调用
func
次数,尽管成本是更多的内存使用。 It could also be optimized a bit into a one-liner return ( (item, (other for other in seq if func(item, other))) for item in seq)
, but I judge that not the most readable way to introduce the code ;-) 它也可以稍微优化一个单行
return ( (item, (other for other in seq if func(item, other))) for item in seq)
,但我判断不是最可读的方式来引入代码;-)
from collections import defaultdict
def group_by_classes(students):
result = defaultdict(list)
for student in students:
result[set(student["courses"])].append(student)
return result
which will result in a list of students for each unique set of classes (ie every occupied vertex of the class hypercube). 这将导致每个唯一类集的学生列表(即类超立方体的每个占用顶点)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.