python中的itertools.groupby() [英] itertools.groupby( ) in python

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

问题描述

我有一组元组.例如:

set([(('E', ('T',)), 0),
 (('F', ('(', 'E', ')')), 0),
 (('T', ('F',)), 0),
 (('__S__', ('E', '$')), 0),
 (('E', ('E', '+', 'T')), 0),
 (('T', ('T', '*', 'F')), 0),
 (('F', ('id',)), 0)])

如您所见,每个元组都有一个元组作为它的第一个元素(例如('F',('(','E',')'))). 该元组的第一个元素是单个字符,第二个元素是另一个元组(例如('(','E',')'))).该元组中具有一个或多个单个字符.
(实际上是上下文无关语法.第一个元素是rule(head)的LHS,第二个元组是RHS(body)
每个元组的第二个元素中的数字是指向此语法的RHS中的字符之一的指针.

我想要做的是根据所指向的元素对这个元组进行分组.
为此,我编写了以下代码:

as you see every tuple has a tuple as it's first element ( ex. ('F', ('(', 'E', ')')) ).
first element of this tuple is single character and second element is another tuple ( ex. ('(', 'E', ')')) ). this tuple has one or more single character in it.
(It is actually Context Free Grammar. first element is LHS of rule(head), second tuple is RHS(body)
number in second element of each tuple is a pointer to one of the characters in RHS of this grammar.

what I am trying to do is grouping this tuples with respect to the element that has been pointed to.
for this purpose I wrote following code:

import itertools
S = set([(('E', ('T',)), 0), (('F', ('(', 'E', ')')), 0), (('T', ('F',)), 0), (('__S__', ('E', '$')), 0), (('E', ('E', '+', 'T')), 0), (('T', ('T', '*', 'F')), 0), (('F', ('id',)), 0)])
for v, h in itertools.groupby(S, lambda x: x[0][1][x[1]] if len(x[0][1]) > x[1] else None ):
     if (v is None):
         continue
     print '--'
     print v
     for hi in h:
         print hi

如果x [0] [1] [x [1]]相同,则两个元组在同一组中. x [0] [1]是第一个元组的第二个元组(语法的RHS),而x [1]是指针.
我得到以下结果:

two tuples are in the same group if x[0][1][x[1]] are the same. x[0][1] is second tuple of first tuple(RHS of grammar) and x[1] is the pointer.
I get following result:

--
(
(('F', ('(', 'E', ')')), 0)
--
F
(('T', ('F',)), 0)
--
E
(('__S__', ('E', '$')), 0)
--
T
(('T', ('T', '*', 'F')), 0)
--
id
(('F', ('id',)), 0)
--
T
(('E', ('T',)), 0)
--
E
(('E', ('E', '+', 'T')), 0)

如您所见,有两个键为"T"的组.我不明白我在做什么错!
我几乎是新的python程序员.万一问题太愚蠢了!
谢谢!

As you can see there is two group with key 'T'. I don't understand what am I doing wrong here!
I am almost new python programmer. In case the problem is too stupid!
thanks!

推荐答案

itertools.groupby()如果要对所有喜欢的数据进行分组,则需要按照

itertools.groupby() requires the data to be sorted if you want all like data to be grouped, as per the documentation:

通常,可迭代项需要已经在同一键上排序 功能.

Generally, the iterable needs to already be sorted on the same key function.

groupby()的操作类似于Unix中的uniq过滤器.它 每次键的值产生一个中断或新的组 函数更改(这就是为什么通常需要进行排序的原因 使用相同按键功能的数据).这种行为不同于 SQL的GROUP BY汇总了常见元素,无论它们如何 输入顺序.

The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function). That behavior differs from SQL’s GROUP BY which aggregates common elements regardless of their input order.

首先(使用函数作为键函数)对数据调用sorted(),然后进行分组.

Simply call sorted() on your data first (using your function as a key function), then do your grouping.

key_func = lambda x: x[0][1][x[1]] if len(x[0][1]) > x[1] else None
itertools.groupby(sorted(data, key=key_func), key_func)

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

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