python 有两个有goupby的库
- pandas(需要numpy)
import pandas as pd
df = pd.DataFrame(log_list, columns=["msg_ori"]) # 先将list转成pd的dataframe,log_list是内容列表,columns是定义的列名索引
df2 = df.groupby(["msg_ori"]).size() #这里结果是pd的series,没找到很好的转dict的方法,直接硬拆字符串了
items = str(df2).split("\n")
result = dict()
for item in items:
try:
if item == "msg_ori":
continue
tmp = item.rsplit(" ", 1)
result[tmp[0]] = tmp[1]
except:
continue
- itertools
from itertools import groupby
from operator import itemgetter
df2 = groupby(log_list, itemgetter(0))
result = dict()
for key, item in df2:
result[key] = str(len(list(item)))