I am using Python 3.4 32 bits on win 7.
I found that an integer in an numpy array has 4 bytes, but in a list it has 10 bytes.
import numpy as np
s = 10;
lt = [None] * s;
cnt = 0 ;
for i in range(0, s):
lt[cnt] = i;
cnt += 1;
lt = [x for x in lt if x is not None];
a = np.array(lt);
print("len(a) is " + str(len(a)) + " size is " + str(sys.getsizeof(a)) \
+ " bytes " + " a.itemsize is " + str(a.itemsize) + " total size is " \
+ str(a.itemsize * len(a)) + " Bytes , len(lt) is " \
+ str(len(lt)) + " size is " + str(sys.getsizeof(lt)) + " Bytes ");
len(a) is 10 size is 40 bytes a.itemsize is 4 total size is 40 Bytes , len(lt) is 10 size is 100 Bytes the fist element has 12 Bytes
Because in a list, each element has to keep a pointer to point to the next element ?
If I assigned a string to the list:
lt[cnt] = "A";
len(a) is 10 size is 40 bytes a.itemsize is 4 total size is 40 Bytes , len(lt) is 10 size is 100 Bytes the fist element has 30 Bytes
So, in array, each element has 4 bytes and in list, it is 30 bytes.
But, if I tried:
lt[cnt] = "AB";
len(a) is 10 size is 40 bytes a.itemsize is 8 total size is 80 Bytes , len(lt) is 10 size is 100 Bytes the fist element has 33 Bytes
In array, each element has 8 bytes but in list, it is 33 bytes.
if I tried :
lt[cnt] = "csedvserb revrvrrw gvrgrwgervwe grujy oliulfv qdqdqafwg5u u56i78k8 awdwfw"; # 73 characters long
len(a) is 10 size is 40 bytes a.itemsize is 292 total size is 2920 Bytes , len(lt) is 10 size is 100 Bytes the fist element has 246 Bytes
In array, each element has 292 bytes (=73 * 4) but in list, it has 246 bytes ?
Any explanation will be appreciated.
first elementsize?sys.getsizeof(lt[0])?