我想这对你来说是更优雅的解决方案。将文件读入pandas数据框,按分组并计算对数。
import pandas as pd
d = [(1,2,3),(1,2,4),(1,2,1),(1,1,5),(1,4,5),(1,1,8)]
cntdt = pd.DataFrame(d,columns=['x','y','cnt'])
cntdt.head()
s = cntdt.groupby(['y','x']).size()
#to get the dataframe
s.to_frame('count').reset_index()
#to get the dictionary
s.to_dict()
字典输出:{(1,1):2,(2,1):3,(4,1):1}
数据帧输出:
<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>y</th> <th>x</th> <th>count</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>1</td> <td>1</td> <td>2</td> </tr> <tr> <th>1</th> <td>2</td> <td>1</td> <td>3</td> </tr> <tr> <th>2</th> <td>4</td> <td>1</td> <td>1</td> </tr> </tbody></table>