# 使用itchat获取微信好友的男女比例 import itchat itchat.auto_login(hotReload=True) friends = itchat.get_friends(update=True)[0:] # 初始化 男和女的数量 male = female = other = 0 for i in friends[1:]: print(i) sex = i["Sex"] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(friends[1:]) # 总数 # % .2f 保留几位有效数字 print("男性比率:%.4f %%" % (male / total * 100)) print("女性比率:%.4f %%" % (female / total * 100)) print("其他比率:%.4f %%" % (other / total * 100)) # 练习:打印出自己的好友性别比率