蟒蛇如何根据属性的对象使用计数器 [英] Python how to use Counter on objects according to attributes
问题描述
我有一个名为记录类,其中存储日志记录的信息;
I have a class named record, which stores information of log record;
class Record():
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
和该记录的例子可以是:
And examples of this record can be:
r1 = Record(uid='001',url='www.google.com',status=200)
r2 = Record(uid='002',url='www.google.com',status=404)
r3 = Record(uid='339',url='www.ciq.com', status=200)
...
我要的是计算每个URL有多少用户了。因此,对谷歌,也有'001'和'002'。我通常使用一个计数器来记录列表,其外观中的元素。但在这里,似乎柜台干脆把元素,而不是指望他们。是否有一个lambda我可以把或尝试?
What I want is to count how many users each url has. So for "google", there are '001' and '002'. I usually use a Counter to record elements within a list and their appearances. But here, Counter seems just put the elements instead of counting them. Is there a lambda I can put or try?
我可以去通过全体员工虽然...
I can go through all the staff though...
我想我可能会在这里引起混乱。
I think i may cause confusion here.
我的重点是通过组其属性的对象。所以,不仅网址计数,而且,
My key point is to group the objects by its attributes...So not only the url counting but also,
res = Counter(r)
(不知道如何把里面的λ,甚至可能的话)我可以得到也许
(don't know how to put lambda inside or even that's possible) I can get maybe
res[0].url = 'www.google.com'
和计数为2 ..?
和建议?
谢谢!
推荐答案
有在我的previous回答一个微妙的bug,虽然修复它,我想出了一个更简单,更快捷的方式做事情不再使用 itertools.groupby()
。
There was a subtle bug in my previous answer, and while fixing it I came up with a much simpler and faster way to do things which no longer uses itertools.groupby()
.
下面现在的更新code特点设计做的正是你想要的功能。
The updated code below now features a function designed to do exactly what you want.
from collections import Counter
from operator import attrgetter
class Record(object):
def __init__(self, **kwargs):
for key, value in kwargs.iteritems():
setattr(self, key, value)
records = [Record(uid='001', url='www.google.com', status=200),
Record(uid='002', url='www.google.com', status=404),
Record(uid='339', url='www.ciq.com', status=200)]
def count_attr(attr, records):
""" Returns Counter keyed by unique values of attr in records sequence. """
get_attr_from = attrgetter(attr)
return Counter(get_attr_from(r) for r in records)
for attr in ('status', 'url'):
print('{!r:>8}: {}'.format(attr, count_attr(attr, records)))
输出:
'status': Counter({200: 2, 404: 1})
'url': Counter({'www.google.com': 2, 'www.ciq.com': 1})
这篇关于蟒蛇如何根据属性的对象使用计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!