Numpy创建2的幂数组

时间:2017-10-09 19:26:43

标签: python arrays numpy

我试过了:

>>>a = np.array(2**np.arange(42)[::-1],dtype = np.uint64)

得到了

>>> a
array([                   0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0, 18446744071562067968,           1073741824,
                  536870912,            268435456,            134217728,
                   67108864,             33554432,             16777216,
                    8388608,              4194304,              2097152,
                    1048576,               524288,               262144,
                     131072,                65536,                32768,
                      16384,                 8192,                 4096,
                       2048,                 1024,                  512,
                        256,                  128,                   64,
                         32,                   16,                    8,
                          4,                    2,                    1], dtype=uint64)

18446744071562067968当然不是2 to the power 31,之后答案当然不是零! 检查我试过的某种溢出错误:

>>> a[0] = 2**42

得到了:

>>> a
array([       4398046511104, ..... 

  the rest the same  

有谁能告诉我出了什么问题?

1 个答案:

答案 0 :(得分:2)

是的,f = open("hello.txt", 'r') dictionary={} for line in f: for word in line.split(): items = line.split() bigrams = [] for i in range(len(items) - 1): bigrams.append((items[i], items[i+1])) my_dict = {i:bigrams.count(i) for i in bigrams} # print(my_dict) with open('bigram.txt', 'wt') as out: out.write(str(my_dict)) f.close() 默认为有符号整数,可能是np.arange(42),可能是因为您使用的是32位架构,或者您使用的是Windows。抛弃完全没用的np.int32包装器,并将dtype传递给np.array

np.arange