访问日志内容:
'asdfasd'|2222|'asdfasdf'|{'phone':'13511020335','name':'lsq','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'13511020335','name':'lsq','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'13810210010','name':'llj','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'18900102987','name':'ljk','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'13810210010','name':'llj','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'13511020335','name':'lsq','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'13511020335','name':'lsq','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'13511020335','name':'lsq','address':'asdfasdfasdfasf'}
'asdfasd'|2222|'asdfasdf'|{'phone':'18900102987','name':'ljk','address':'asdfasdfasdfasf'}
统计需求:按电话和用户名来统计访问次数
可以用三种方法:
### 1. collections Counter
from collections import Counter
l = []
for s in open('a.txt'):
d = eval(s.split('|')[-1].strip())
l.append(d['phone']+ '||' + d['name'])
Counter(l)
Counter({'13511020335||lsq': 5, '18900102987||ljk': 2, '13810210010||llj': 2})
### 2.list.count
l = []
for s in open('a.txt'):
d = eval(s.split('|')[-1].strip())
l.append(d['phone']+ '||' + d['name'])
a = {}
for i in l:
if l.count(i)>=1:
a[i] = l.count(i)
print a
>>> print a
{'18900102987||ljk': 2, '13810210010||llj': 2, '13511020335||lsq': 5}
### 3.dict in
dr = {}
for s in open('a.txt'):
d = eval(s.split('|')[-1].strip())
k_dr = d['phone']+ "||" + d['name']
if k_dr in dr:
dr[k_dr] = dr[k_dr] + 1
else:
dr[k_dr] = 1
>>> print dr
{'18900102987||ljk': 2, '13810210010||llj': 2, '13511020335||lsq': 5}