位数统计问题:获取一系列数据,统计bit数,并画出直方图

"""
查看所有数据有多少bit
"""
import collections
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

with open("f.txt") as f:
    x = f.readlines()

digs = []
for i in x:
    if len(i.split(" ")) > 1 :
        #print(i.split(" ")[1].split("\n")[0])
        d = i.split(" ")[1].split("\n")[0]
        if (int(d) > 2**15):
            print(d, " more than 15 bits ")

        digs.append(len(bin(int(d))) - 2) # '0b' minus

statis = collections.Counter(digs)
print(statis)
# plt.hist(digs) #固定划分10个bins,这里我们要遍历,不知道多少bins,不合适
plt.bar(statis.keys(), statis.values())
plt.show()

print("max ", max(digs))

run

Counter({12: 221, 13: 215, 11: 195, 14: 174, 10: 152, 9: 148, 8: 103, 15: 93, 7: 69, 6: 50, 5: 33, 1: 29, 4: 25, 3: 16, 2: 13})
max 15

input: f.txt file

f0
0 0
1 50
2 50
3 101
4 101
5 151
6 151
7 201
8 201
9 251
10 251
11 302
12 302
13 352
14 352
15 402
16 402
17 452
18 452
19 503
20 503
21 553
22 553
23 603
24 603
25 653
26 653
27 704
28 704
29 754
30 754
31 804
32 804
33 855
34 855
35 905
36 905
37 955
38 955
39 1005
40 1005
41 1056
42 1056
43 1106
44 1106
45 1156
46 1156
47 1206
48 1206
49 1257
50 1257
51 1307
52 1307

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值