码迷,mamicode.com
首页 > 其他好文 > 详细

关于tuple 和 list 看点基础的东东

时间:2020-06-26 22:04:17      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:square   loading   help   append   mmu   remove   turn   ever   内存   

当大量数据时,尽量用tuple
print 80*‘-‘

print sys.getsizeof(objiect)

print dir(sys)
print help(sys.getsizeof)

tuple 的快速写法:
test1 = 1,
test2 = 1
test3 = 1,2,3

a = test3[0]
b = test3[1]
a, b, c = test3

list: [‘append‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
tuple: [‘count‘, ‘index‘]

所以 list 占用内存多
imagine working with a data set containing 100 million lists or tuples, the bytes quickly add up

另外,list 可变, tuple 不可变immutable
once you make a tuple, it is set in stone

tuples 比 list 更快创建
tuples can be made more quickly than lists

import sys

print dir(sys)

print help(sys.getsizeof)

‘‘‘
getsizeof(...)
    getsizeof(object, default) -> int
    
    Return the size of object in bytes.
‘‘‘

list_eg = [1,2,3,"a","b","c",True,3.14159]
tuple_eg =(1,2,3,"a","b","c",True,3.14159)


print "List size = ", sys.getsizeof(list_eg)
print "tuple size = ", sys.getsizeof(tuple_eg)

技术图片

import timeit

list_test = timeit.timeit(stmt = "[1,2,3,4,5]", number = 1000000)
tuple_test = timeit.timeit(stmt = "(1,2,3,4,5)", number = 1000000)

print "List time: ", list_test
print "Tuple time: ", tuple_test

技术图片

test1 = 1,
test2 = 1
test3 = 1,2,3

print test1
print test2
print test3

a, b ,c = test3
print a,b,c

d = test2
print d

技术图片

还有个 横线写法,我都是按一串 -----

print dir(prime_numbers)

print 80 * "-"

print dir(perfect_squares)

关于tuple 和 list 看点基础的东东

标签:square   loading   help   append   mmu   remove   turn   ever   内存   

原文地址:https://www.cnblogs.com/vivivi/p/13196460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!