首页 > 代码库 > Python-元组(tuple),文件
Python-元组(tuple),文件
基础:
1. 元组由简单对象组构成。
2. 元组与列表类似,不过不能在原处修改(它们是不可变的),并且通常写成圆括号(),而不是方框号[]中的一系列项。
==========================================================================
>>> (1,2) + (3,4)
(1,2,3,4)
>>> (1,2)*4
(1,2,1,2,1,2,1,2)
>>> T = (1,2,3,4)
>>> T[0], T[1:3]
(1, (2,3))
===========================================================================
>>> x = (40) # An integer!
>>> x
40
>>> y = (40,) #A tuple containing an integer
>>> y
(40,)
注:在不会引发语法冲突的情况系啊,Python允许忽略元组的圆括号。
如果圆括号里的单一对象是元组对象而不是一个简单的表达式(圆括号可以把表达式括起来), 需要对Python进行特别说明。
如果确实像得到一个远足,只要在这一单个元素之后,关闭圆括号之前加一个逗号即可。
=============================================================================
>>> T = (‘cc‘, ‘aa‘, ‘dd‘, ‘bb’)
>>> tmp = list(T) # Make a list from a tuple‘s items
>>> tmp.sort()
>>> tmp
[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘]
>>> T = tuple(tmp) # Make a tumple from the list‘s items
>>> T
(‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘)
>>> sorted(T) # Or use the sorted built-in
[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘]
=============================================================================
>>> T = (1, [2,3], 4)
>>> T[1] = ‘spam‘ # This fails: can‘t change tuple itself
TypeError
>>> T[1][0] = ‘spam‘ # This works: can change mutables inside
>>> T
(1, [‘spam‘, 3], 4)
=============================================================================
eval能够把字符串当做可执行程序代码(从技术上来讲,就是一个含有Python表达式的字符串)
>>> line = "[1,2,3]${‘a‘:1, ‘b‘:2}\n"
>>> line
"[1,2,3]${‘a‘:1, ‘b‘:2}\n"
>>> parts = line.split(‘$‘)
>>> parts
[‘[1,2,3]‘,"{‘a‘:1, ‘b‘:2}\n"]
>>> eval(parts[0])
[1,2,3]
>>> objects = [eval(P) for P in parts]
>>> objects
[[1,2,3], {‘a‘:1, ‘b‘:2}]
=========================================================
用pickle存储Python的原生对象
pickle与eval的区别:
eval能够执行Python的任何表达式,有可能会删除计算机上所有文件的表达式;
pickle能够存储Python原生对象,也不要求把字符串转换。
>>> D = {‘a‘: 1, ‘b‘: 2}
>>> F = open(‘datafile.pkl‘, ‘wb‘)
>>> import pickle
>>> pickle.dump(D, F) # Pickle any object to file
>>> F.close()
之后想要取回字典时,只要简单的再用一次pickle进行重建就可以了;
>>> F = open(‘datafile.pkl‘, ‘wb‘)
>>> E = pickle.load(F)
>>> E
{‘a‘: 1, ‘b‘: 2}
>>> open(‘datafile.pkl‘, ‘rb‘).read() # Format is prone to change!
b‘\x90\x03}q\x00(X\x01\x00\x00…….‘
注:在这里是使用二进制模式打开用于储存pickle化的对象的文件,因为二进制模式总是Python 3.X中必须的,同时pickle程序创建和使用一个bytes字符串对象,并且这些对象意味着二进制模式文件(文本模式文件意味着Python 3.X中的str字符串)
===========================================================
struct模块能够创造并解析打包的二进制数据;
从某种意义上说,它是另一个数据转换工具。
>>> F = open(‘data.bin‘, ‘wb‘)
>>> import struct
>>> data = http://www.mamicode.com/struct.pack(‘>i4sh‘, 7, ‘spam‘, 8) # Make packed binary data
>>> data
b‘\x00\x00\x00\x07spam\x00\x08‘
>>> F.wirte(data) # Write byte string
>>> F.close()
>>> F = open(‘data.bin‘, ‘rb‘)
>>> data = http://www.mamicode.com/F.read()
>>> data
b‘\x00\x00\x00\x07spam\x00\x08‘
>>> values = struct.unpack(‘>i4sh‘, data) # Convert to Python objects
>>> values
(7, ‘spam‘, 8)
=========================================================
引用:
>>> X = [1,2,3]
>>> L = [‘a‘, X, ‘b‘]
>>> D = {‘x‘: X, ‘y‘: 2}
>>> X[1] = ‘surprise‘
>>> L
[‘a‘, [1, ‘surprise‘, 3], ‘b‘]
>>> D
{‘x‘: [1, ‘surprise‘, 3], ‘y‘: 2}
复制:
>>> L = [1,2,3]
>>> D = {‘a‘: 1, ‘b‘: 2}
>>> A = L[:]
>>> B = D.copy()
>>> A[1] = ‘N1‘
>>> D[‘c‘] = ‘spam‘
>>> L,D
([1,2,3], {‘a‘: 1, ‘c‘: ‘spam‘, ‘b‘: 2})
>>> A,B
([1, ‘Ni‘, 3], {‘a‘: 1, ‘b‘: 2})
如果将引用中修改为
>>> X = [1,2,3]
>>> L = [‘a‘: X[:], ‘b‘]
>>> D = {‘x‘: X[:], ‘y‘: 2]
则X的修改不会影响到L与D
========================================================
判断“一致性”与“相等性”
>>> L1 = [1,(‘a‘, 3)]
>>> L2 = [1,(‘a‘, 3)]
>>> L1 == L2, L1 is L2
(True, False)
“==”判断相等性,“is”判断一致性;
>>> S1 = ‘spam‘
>>> S2 = ‘spam‘
>>> S1 == S2, S1 is S2
(True, True)
>>> S1 = ‘a longer string‘
>>> S2 = ‘a longer string‘
>>> S1 == S2, S1 is S2
(True, false)
理由:
Python内部暂时存储并重复使用段字符串作为最佳化,因为‘spam‘实际上在内存中只存在一个字符串用于分享;
Python-元组(tuple),文件