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

当大量数据时,尽量用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)
posted @ 2020-06-26 21:35  vivi~  阅读(143)  评论(0编辑  收藏  举报