itertools.groupby [英] itertools.groupby

查看:72
本文介绍了itertools.groupby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bejeezus。 docs中对groupby的描述是一个海报孩子

为什么文档需要用户评论。可以有人向我解释

这个例子中使用的名称''uniquekeys''是什么意思:

import itertools


mylist = ['''',1,''b'',2,3,''c'']


def isString(x):

s = str(x)

如果s == x:

返回True

else:

返回False


uniquekeys = []

groups = []

for k,g in itertools.groupby(mylist,isString ):

uniquekeys.append(k)

groups.append(list(g))


print uniquekeys
打印组


- 输出: -

[真,假,真,假,真]

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

Bejeezus. The description of groupby in the docs is a poster child
for why the docs need user comments. Can someone explain to me in
what sense the name ''uniquekeys'' is used this example:
import itertools

mylist = [''a'', 1, ''b'', 2, 3, ''c'']

def isString(x):
s = str(x)
if s == x:
return True
else:
return False

uniquekeys = []
groups = []
for k, g in itertools.groupby(mylist, isString):
uniquekeys.append(k)
groups.append(list(g))

print uniquekeys
print groups

--output:--
[True, False, True, False, True]
[[''a''], [1], [''b''], [2, 3], [''c'']]

推荐答案

5月27日上午11点28分,Steve Howell< showel ... @ yahoo.comwrote:
On May 27, 11:28 am, Steve Howell <showel...@yahoo.comwrote:

--- 7stud< bbxx789_0 ... @ yahoo.comwrote:
--- 7stud <bbxx789_0...@yahoo.comwrote:

Bejeezus。 docs中对groupby的描述是

海报孩子

为什么文档需要用户评论。可以有人

向我解释

使用这个名称''uniquekeys''是什么意思这个

例子:[...]
Bejeezus. The description of groupby in the docs is
a poster child
for why the docs need user comments. Can someone
explain to me in
what sense the name ''uniquekeys'' is used this
example: [...]



groupby方法有它的用途,但它的行为是

对于任何使用过的人来说都是非常令人惊讶的>
group by SQL的语法,因为Python的groupby

方法会重复组,如果你的数据没有排序,

而SQL有奢侈的(知道它是' )

使用有限的数据集,因此它可以提供更方便的语义。


__________________________________________________ _________________________ _________你打盹,你输了。使用AutoCheck尽快获取消息

在全新Yahoo!中邮件测试版。 http://advision.webevents.yahoo.com/。 ..mail_html.html


groupby方法的用途是
The groupby method has its uses



我'' d解决它在python中的作用的简单解释。

I''d settle for a simple explanation of what it does in python.


On Sun,2007-05-27 at 10:17 -0700,7stud写道:
On Sun, 2007-05-27 at 10:17 -0700, 7stud wrote:

Bejeezus。 docs中对groupby的描述是一个海报孩子

为什么文档需要用户评论。可以有人向我解释

这个例子使用的名称''uniquekeys''是这样的:


import itertools


mylist = ['''',1,''b'',2,3,''c'']


def isString(x):

s = str(x)

如果s == x:

返回True

else:

返回False


uniquekeys = []

groups = []

for k,g in itertools.groupby (mylist,isString):

uniquekeys.append(k)

groups.append(list(g))


打印uniquekeys

打印组


- 输出: -

[真,假,真,假,真]

[[''a''],[1],[''b''],[2,3],[''c'']]
Bejeezus. The description of groupby in the docs is a poster child
for why the docs need user comments. Can someone explain to me in
what sense the name ''uniquekeys'' is used this example:
import itertools

mylist = [''a'', 1, ''b'', 2, 3, ''c'']

def isString(x):
s = str(x)
if s == x:
return True
else:
return False

uniquekeys = []
groups = []
for k, g in itertools.groupby(mylist, isString):
uniquekeys.append(k)
groups.append(list(g))

print uniquekeys
print groups

--output:--
[True, False, True, False, True]
[[''a''], [1], [''b''], [2, 3], [''c'']]



您从文档中引用的所谓示例并不是使用itertools.groupby的实际示例,而是建议代码如何使用

存储groupin g如果你需要迭代两次,因为迭代器

通常是不可重复的。


因此,''uniquekeys''列出了关键值对应于每个组

''groups''。 groups [0]是分组在

uniquekeys [0]下的元素列表,groups [1]是在

uniquekeys [1]等下分组的元素列表。您获得了令人惊讶的结果,因为您的数据

未按组密钥排序。你的组密钥在True

和False之间交替。


也许你需要向我们解释你实际上想要做什么。

用户提供的文档评论对此没有帮助。


问候,


-

Carsten Haese
http://informixdb.sourceforge.net


7stud写道:
7stud wrote:

Bejeezus。 docs中对groupby的描述是一个海报孩子

为什么文档需要用户评论。可以有人向我解释

这个例子中使用的名称''uniquekeys''是什么意思:
Bejeezus. The description of groupby in the docs is a poster child
for why the docs need user comments. Can someone explain to me in
what sense the name ''uniquekeys'' is used this example:



这是我第一次接触到这个函数,我看到它在我的代码中确实有一些用途。然而,我同意这是令人困惑的。

IMO如果具有当前

行为的函数被重命名为'telescope''或''compact',则可以减少混淆'或''崩溃''或

某事(因为它在可变的线性上折叠了同质的

序列。)

一个名为groupby的函数可以那么我认为是明确的

隐含的行为,即在输入列表中为每个唯一类型的

事物创建一个迭代器,按键函数分类。 br />

This is my first exposure to this function, and I see that it does have
some uses in my code. I agree that it is confusing, however.
IMO the confusion could be lessened if the function with the current
behavior were renamed ''telescope'' or ''compact'' or ''collapse'' or
something (since it collapses the iterable linearly over homogeneous
sequences.)
A function named groupby could then have what I think is the clearly
implied behavior of creating just one iterator for each unique type of
thing in the input list, as categorized by the key function.


这篇关于itertools.groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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