利用python字典统计次数

node2:/root/python3#cat p1.py 
from collections import defaultdict
import psutil
import sys
stats = defaultdict(int)
dict1={}
print(stats)
print(type(stats))
print('111111111111111111111')
print(type(psutil.net_connections()))
print(sys.getsizeof(psutil.net_connections()))
print('222222222222222222222')

for conn in psutil.net_connections('tcp'):
   print(conn.status)
   try:
    dict1[conn.status]
   except:
    dict1[conn.status]=0
   finally:
     dict1[conn.status]=dict1[conn.status]+1
print(dict1)


node2:/root/python3#python3 p1.py 
defaultdict(, {})

111111111111111111111

544
222222222222222222222
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN

你可能感兴趣的:(python,进阶)