关于tuple 和 list 看点基础的东东
Posted vivivi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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)
以上是关于关于tuple 和 list 看点基础的东东的主要内容,如果未能解决你的问题,请参考以下文章
java中LIST 出来的东东 作为参数放进一个方法,方法里改变了LIST,那是否就真的改变了?还是出了这个方法