请教一个二维数组的数据处理.

2019-02-25 16:51:37 +08:00
 coolloves
分析 nginx 的日志,目前把把数据按照需求,处理成[(requesturl1,responsetime1),(requesturl2,responsetime2),.......].
现在想要统计下 url 的次数,以及 avg responsetime,并排序.
我能想到的就是 for 循环,感觉有点 low,请教大家一下更好的办法,多谢!


a =[["d",2],["c",5],["a",9],["b",4],["b",2],["c",9]]
uniqs = list(set([x[0] for x in a]))
res = {}
for i in uniqs:
    count = 0
    sumtime = 0
    for j in a:
        if i == j[0]:
            count = count + 1
            sumtime = sumtime + j[1]
        res[i] = [count,sumtime]
lists = []
for i in res.keys():
    lists.append((i,res[i][0],res[i][1]/res[i][0]))
print(sorted(lists,key=lambda x:x[1],reverse=True))

1646 次点击
所在节点    Python
8 条回复
chenstack
2019-02-25 21:42:02 +08:00
稍微减少了几行
from itertools import groupby
from operator import itemgetter

a = [["d", 2], ["c", 5], ["a", 9], ["b", 4], ["b", 2], ["c", 9]]

a = sorted(a, key=itemgetter(0))
lists = []

for key, group in groupby(a, itemgetter(0)):
    time_list = [item[1] for item in group]
    lists.append((key, len(time_list), sum(time_list) / len(time_list)))

print(sorted(lists, key=itemgetter(1), reverse=True))
zzz686970
2019-02-25 22:25:43 +08:00
```py
import collections
a = [["d", 2], ["c", 5], ["a", 9], ["b", 4], ["b", 2], ["c", 9]]
res = collections.defaultdict(list)
x, y = zip(*a)
for i in range(len(x)):
res[x[i]] += y[i],

print(sorted([(key, len(value), sum(value)/len(value)) for key, value in res.items()] , key = lambda x: (x[1]), reverse = True))
```
试了一下 collections
necomancer
2019-02-26 13:26:26 +08:00
import pandas as pd
a =[["d",2],["c",5],["a",9],["b",4],["b",2],["c",9]]
d = pd.DataFrame(a)
[ (_[0], _[1].mean().get_values()) for _ in d.groupby(0)]

Out:

[('a', array([9.])),
('b', array([3.])),
('c', array([7.])),
('d', array([2.]))]
chenstack
2019-02-26 14:33:26 +08:00
似乎还能更短, 要用 Python3
from itertools import groupby
from operator import itemgetter

a = [["d", 2], ["c", 5], ["a", 9], ["b", 4], ["b", 2], ["c", 9]]

print(sorted([(key, len(group), sum(item[1] for item in group) / len(group))
    for key, (*group,) in groupby(sorted(a, key=itemgetter(0)), itemgetter(0))], key=itemgetter(1), reverse=True))
coolloves
2019-02-26 15:52:53 +08:00
@necomancer 请教,pd 这个没有次数统计.如何统计呢,我也在看 pandas
necomancer
2019-02-26 16:06:31 +08:00
@coolloves groupby 不就是次数统计了么……你想实现什么的计数? pandas 有 unique 和 nunique 方法。
princelai
2019-02-28 10:41:39 +08:00
```
import pandas as pd
a =[["d",2],["c",5],["a",9],["b",4],["b",2],["c",9]]
df = pd.DataFrame(a)
df.groupby(0).agg(["count","mean"])
```

Out[5]:
1
count mean
0
a 1 9
b 2 3
c 2 7
d 1 2
coolloves
2019-02-28 14:16:02 +08:00
@princelai 谢谢,还是 pd 最简洁

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/538552

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX