Python的列表推导(理想情况下)在SQL中是否等同于“count(*)... group by ...”? [英] Can Python's list comprehensions (ideally) do the equivalent of 'count(*)...group by...' in SQL?
问题描述
我认为列表推导可能会给我这个,但我不知道:在Python(2.6)中的任何优雅的解决方案一般用于在列表中选择唯一的对象,并提供计数?
(我定义了一个 __ eq __
来定义我的对象定义的唯一性)。
所以在RDBMS-land中,这样的:
CREATE TABLE x(n NUMBER(1));
INSERT INTO x VALUES(1);
INSERT INTO x VALUES(1);
INSERT INTO x VALUES(1);
INSERT INTO x VALUES(2);
SELECT COUNT(*),n FROM x
GROUP BY n;
其中:
COUNT(*)n
==========
3 1
1 2
所以,这里是我在Python中的等效列表:
1,1,1,2]
我想要的输出与SQL SELECT给出的输出相同。
编辑:我在这里给出的例子是简化的,我实际上是处理用户定义的对象实例的列表:只是为了完整性我包括我需要的额外的代码让整个工作:
import hashlib
def __hash __(self):
md5 = hashlib.md5()
[md5.update(i)for i in self.my_list_of_stuff]
return int(md5.hexdigest(),16)
需要 __ hash __
方法才能获得 code>转换为工作(我选择了在2.6中工作的列表理解概念[尽管事实上,我知道涉及到低效率(见注释) - 我的数据集足够小,不是一个问题] )。
my_list_of_stuff
上面是我的对象定义上的(字符串)列表。
>>> values = [1,1,1,2]
pre>
>>>> print [(x,values.count(x))for x in set(values)]
[(1,3),(2,1)]
如S.Lott提到的,一个defaultdict可以做同样的事情。
I think list comprehensions may give me this, but I'm not sure: any elegant solutions in Python (2.6) in general for selecting unique objects in a list and providing a count?
(I've defined an
__eq__
to define uniqueness on my object definition).So in RDBMS-land, something like this:
CREATE TABLE x(n NUMBER(1)); INSERT INTO x VALUES(1); INSERT INTO x VALUES(1); INSERT INTO x VALUES(1); INSERT INTO x VALUES(2); SELECT COUNT(*), n FROM x GROUP BY n;
Which gives:
COUNT(*) n ========== 3 1 1 2
So , here's my equivalent list in Python:
[1,1,1,2]
And I want the same output as the SQL SELECT gives above.
EDIT: The example I gave here was simplified, I'm actually processing lists of user-defined object-instances: just for completeness I include the extra code I needed to get the whole thing to work:
import hashlib def __hash__(self): md5=hashlib.md5() [md5.update(i) for i in self.my_list_of_stuff] return int(md5.hexdigest(),16)
The
__hash__
method was needed to get theset
conversion to work (I opted for the list-comprehension idea that works in 2.6 [despite the fact that I learnt that involves an inefficiency (see comments) - my data set is small enough for that not be an issue]).my_list_of_stuff
above is a list of (Strings) on my object definition.解决方案Lennart Regebro provided a nice one-liner that does what you want:
>>> values = [1,1,1,2] >>> print [(x,values.count(x)) for x in set(values)] [(1, 3), (2, 1)]
As S.Lott mentions, a defaultdict can do the same thing.
这篇关于Python的列表推导(理想情况下)在SQL中是否等同于“count(*)... group by ...”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!