閱讀929 返回首頁    go Python


Python查看微信好友男女比例

1. 微信好友男女比例

想統計下自己微信裏好友的性別比例,當然也是很簡單,先獲取好友列表,統計列表裏性別計數

import itchat

# 先登錄

itchat.login()

# 獲取好友列表

friends = itchat.get_friends(update=True)[0:]

# 初始化計數器,有男有女,當然,有些人是不填的

male = female = other = 0

# 遍曆這個列表,列表裏第一位是自己,所以從"自己"之後開始計算

# 1表示男性,2女性

for i in friends[1:]:

sex = i["Sex"]

if sex == 1:

male += 1

elif sex == 2:

female += 1

else:

other += 1

# 總數算上,好計算比例啊~

total = len(friends[1:])

# 好了,打印結果

print u"男性好友:%.2f%%" % (float(male) / total * 100)

print u"女性好友:%.2f%%" % (float(female) / total * 100)

print u"其他:%.2f%%" % (float(other) / total * 100)

好看看結果:

pip install echarts-python

展示比例一般使用百分比圓餅表吧

# 使用echarts,加上這段

from echarts import Echart, Legend, Pie

chart.use(Pie('WeChat',

[{'value': male, 'name': u'男性 %.2f%%' % (float(male) / total * 100)},

{'value': female, 'name': u'女性 %.2f%%' % (float(female) / total * 100)},

{'value': other, 'name': u'其他 %.2f%%' % (float(other) / total * 100)}],

radius=["50%", "70%"]))

chart.use(Legend(["male", "female", "other"]))

del chart.json["xAxis"]

del chart.json["yAxis"]

chart.plot()

本文由Python凡夢賬號發布,2017年8月24日

最後更新:2017-10-08 15:42:00

  上一篇:go Python正則表達式
  下一篇:go Python 字典操作進階