简体   繁体   English

列表理解中“groupby”的“if”条件

[英]"if" condition on "groupby" in list comprehension

I have a sequence in which I want to count runs (ie consecutive identical entries), and return a list of the length of the runs.我有一个序列,我想在其中计算运行(即连续的相同条目),并返回运行长度的列表。 The code below下面的代码

from itertools import groupby

S = [1,1,1,2,3,3,4,5,5]

grouped_S = [sum(1 for i in group) for key,group in groupby(S)]

Results, as it should, in结果,正如它应该的那样,在

[3, 1, 2, 1, 2]

But I want to ignore spells that are 1-long, and get output just [3,2,2].但我想忽略长度为 1 的法术,并仅获得 [3,2,2] 的输出。 This这个

grouped_S = [sum(1 for i in group) for key,group in groupby(L) if sum(1 for i in g) >1]

gives

[0,0,0]. 

It clearly knows I want just the three sequences > 1, but won't return their length.它清楚地知道我只想要三个序列 > 1,但不会返回它们的长度。

I don't understand this behavior, could someone please explain?我不明白这种行为,有人可以解释一下吗? Right now my solution is:现在我的解决方案是:

S = [sum(1 for i in g) for k,g in groupby(S)]
S = [i for i in S if i != 1]

and it works, but there has to be a pythonic one-liner I can't figure out.它有效,但必须有一个我无法弄清楚的pythonic one-liner。

As the documentation of groupby points out:正如groupby的文档指出的那样:

The returned group is itself an iterator that shares the underlying iterable with groupby() .返回的组本身就是一个迭代器,它与groupby()共享底层的可迭代对象。

You can only iterate an iterator once, which you're doing in the if ;你只能迭代一个迭代器,你在if做的; there's nothing left in the iterator to sum again then.迭代器中没有任何东西可以再次sum It would be far easier to simply filter the 1 s out of the result:简单地从结果中过滤掉1会容易得多:

grouped_S = list(filter(lambda s: s > 1, (sum(1 for i in g) for k,g in groupby(S))))

You could very well save your variable in a list comprehension and then check it afterwards:您可以很好地将变量保存在列表理解中,然后再进行检查:

from itertools import groupby

S = [1,1,1,2,3,3,4,5,5]
grouped_S = [group
             for k,g in groupby(S)
             for group in [sum(1 for i in g)]
             if group > 1]

print(grouped_S)

This yields这产生

[3, 2, 2]

as @deceze already pointed your variable g is an iterator:因为@deceze 已经指出你的变量g是一个迭代器:

from the docs :文档

iterator迭代器

An object representing a stream of data.表示数据流的对象。 Repeated calls to the iterator's next () method (or passing it to the built-in function next()) return successive items in the stream.重复调用迭代器的next () 方法(或将其传递给内置函数 next())会返回流中的连续项。 When no more data are available a StopIteration exception is raised instead.当没有更多数据可用时,会引发 StopIteration 异常。 At this point, the iterator object is exhausted and any further calls to its next () method just raise StopIteration again print([len(g) for k, [*g] in groupby(S) if len(g) > 1])在这一点上,迭代器对象已耗尽,任何对其next () 方法的进一步调用只会再次引发 StopIteration print([len(g) for k, [*g] in groupby(S) if len(g) > 1] )

you can make your g variable a list by using iterable unpacking operator :您可以使用可迭代解包运算符使g变量成为列表:

print([len(g) for k, [*g] in groupby(S) if len(g) > 1])

output:输出:

[3, 2, 2]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM