python 基本数据类型占用内存空间大小的实例

yipeiwu_com5年前Python基础

python中基本数据类型和其他的语言占用的内存空间大小有很大差别

import sys
a = 100
b = True
c = 100L
d = 1.1
e =""
f = []
g =()
h = {}
i = set([])

print " %s size is %d "%(type(a),sys.getsizeof(a))
print " %s size is %d "%(type(b),sys.getsizeof(b))
print " %s size is %d "%(type(c),sys.getsizeof(c))
print " %s size is %d "%(type(d),sys.getsizeof(d))
print " %s size is %d "%(type(e),sys.getsizeof(e))
print " %s size is %d "%(type(f),sys.getsizeof(f))
print " %s size is %d "%(type(g),sys.getsizeof(g))
print " %s size is %d "%(type(h),sys.getsizeof(h))
print " %s size is %d "%(type(i),sys.getsizeof(i))

 <type 'int'> size is 12 
 <type 'bool'> size is 12 
 <type 'long'> size is 14 
 <type 'float'> size is 16 
 <type 'str'> size is 21 
 <type 'list'> size is 36 
 <type 'tuple'> size is 28 
 <type 'dict'> size is 140 
 <type 'set'> size is 116 

以上这篇python 基本数据类型占用内存空间大小的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django用户认证系统 组与权限解析

Django的权限系统很简单,它可以赋予users或groups中的users以权限。 Django admin后台就使用了该权限系统,不过也可以用到你自己的代码中。 User对象具有两...

python 多线程串行和并行的实例

如下所示: #coding=utf-8 import threading import time import cx_Oracle from pprint import pprint...

Python实现删除时保留特定文件夹和文件的示例

实现功能:删除当前目录下,除保留目录和文件外的所有文件和目录 #!bin/env python import os import os.path import shutil def...

pygame实现简易飞机大战

利用pygame实现了简易版飞机大战。源代码如下: # -*- coding:utf-8 -*- import pygame import sys from pygame.local...

Python sklearn KFold 生成交叉验证数据集的方法

源起: 1.我要做交叉验证,需要每个训练集和测试集都保持相同的样本分布比例,直接用sklearn提供的KFold并不能满足这个需求。 2.将生成的交叉验证数据集保存成CSV文件,而不是直...