Python学习笔记(八)- 元组、文件和其他任何东西(Tuples, Files, and Everything Else)

1.你如何判断元组有多大?为什么这个工具位于它所在的位置?
答:内建函数 len 返回Python中任何容器对象的长度(包含项的数量),包括元组。它是一个内置函数,而不是类型方法,因为它适用于许多不同类型的对象。通常情况下,内建函数和表达式可以横跨多个对象类型;方法特定于单个对象类型,虽然其中一些可以在多个类型上使用(例如,index方法在列表和元组上起作用)。


2.写一个表达式,改变元组的第一项。在这个过程中,(4, 5, 6)应该变为(1, 5, 6)。
答:因为元组是不可变的,事实上,你不能就地改变元组,但是你可以生成带有预期值的心愿组。假设T = (4, 5, 6),你可以通过切片和拼接产生新元组的方式改变元组的第一项:T = (1,) + T[1:]。(回忆一下,单个项的元组需要一个紧跟的逗号)你也可以把元组转换为列表,改变它,再转化为元组,但是这操作有点过头和实践中很少用到——如果你知道对象将要改变,简单地使用列表即可。

3.在文件open调用中,处理模式参数的默认情况是什么?
答:在文件open调用中,默认的处理模式参数是 'r',用来读文本输入。对于输入文本文件,只需传入外部文件的名称即可。

4.不把Python对象转化为字符串的情况下,你可以用什么模块储存它们?
答:pickle模块可以用于将Python对象存储在文件中,而无需显式地将它们转换为字符串。struct模块是相关的,但它假设数据是文件中的打包二进制格式;json类似地按照JSON格式将有限的Python对象转换成字符串。

5.你如何一次复制嵌套结构的所有部分?
答:导入copy模块,如果你需要复制嵌套结构的所有部分,调用 copy.deepcopy(X)。实践中,这也是很少看见的,引用通常是预期的表现,浅拷贝经常对大部分副本足够了(如,aList[:],aDict.copy(),set(aSet))。


6.Python什么情况下认为一个对象是真(True)?
答:如果是非零数或非空集合对象,对象被认为是真(True)。内置单词 True 和 False 本质上是预先定义的,分别具有与整数1和0相同的含义。

7.你现在的任务什么?
答:可接受的答案包括“学习Python”、“继续书的下一部分”或“寻求圣杯”。

扫描二维码关注公众号,回复: 5283098 查看本文章

NOTE:

赋值创建引用(references)而不是副本(copies)

例如,在下面这个例子中,列表赋值给了L和外面再嵌套一层列表赋给了M,改变L会同时影响M
>>>L = [1, 2, 3]
>>>M = ['X', L, 'Y']
>>>M
['X', [1, 2, 3], 'Y']
>>>L[1] = 0
>>>M
['X', [1, 0, 3], 'Y']
这种效果通常只在较大的程序中才会变得重要,而这样的共享引用(shared references)通常正是您想要的。如果对象以不想要的方式在你下面发生变化,你可以通过显式复制它们以避免共享对象。对于列表来说,你可以通过空列表切片复制一个顶级的副本:
>>>L = [1, 2, 3]
>>>M = ['X', L[:], 'Y']
>>>L[1] = 0
>>>L
[1, 0, 3]
>>>M
['X', [1, 2, 3], 'Y']

重复(repetition)增加一层深度(deep)

重复序列就像把它加上几次。然而,当不可变序列被嵌套是,效果可能不是你预期的那样。例如,在下面的例子中,列表L被重复4次,赋给了X,而嵌套列表 [L] 被重复4次赋给了Y:
>>>L = [4, 5, 6]
>>>X = L * 4
>>>Y = [L] * 4
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
因为L是在第二次重复中被嵌套的,Y使嵌入引用返回分配给L的原始列表,因此会出现上一节中提到的相同类型的副作用:
>>> L[1] = 0
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 0, 6], [4, 0, 6], [4, 0, 6], [4, 0, 6]]

这可能看起来是人为的和学术性的——直到它意外地发生在您的代码中!此问题的解决方案与上一节相同,因为这实际上是另一种创建共享可变对象引用情况——在不希望共享引用时复制副本:
>>> L = [4, 5, 6]
>>> Y = [list(L)] * 4 # 嵌套L的副本
>>> L[1] = 0
>>>L
[4, 0, 6]
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]

更微妙的是,尽管Y不再与L共享一个对象,但它仍然嵌入了对同一副本的四个引用。如果您也必须避免这种共享,您将希望确保每个嵌入副本是独一无二的:
>>> Y[0][1] = 99        # 所有的四个副本是相同的
>>> Y
[[4, 99, 6], [4, 99, 6], [4, 99, 6], [4, 99, 6]]
>>> L = [4, 5, 6]
>>> Y = [list(L) for i in range(4)]       #列表推导式创建不同的副本
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
>>> Y[0][1] = 99
>>> Y
[[4, 99, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
如果您还记得重复(repetition)、连接(concatenation)和切片(slice)复制只在操作数(operand)对象的顶层(top level),那么这类情况就更有意义了。

标注:转载《Learning Python 5th Edition》[奥莱理]
1. How can you determine how large a tuple is? Why is this tool located where it is?
2. Write an expression that changes the first item in a tuple. (4, 5, 6) should become (1, 5, 6) in the process.
3. What is the default for the processing mode argument in a file open call?
4. What module might you use to store Python objects in a file without converting them to strings yourself?
5. How might you go about copying all parts of a nested structure at once?
6. When does Python consider an object true?
7. What is your quest?

1. The built-in len function returns the length (number of contained items) for any container object in Python, including tuples. It is a built-in function instead of a type method because it applies to many different types of objects. In general, builtin functions and expressions may span many object types; methods are specific to a single object type, though some may be available on more than one type (index, for example, works on lists and tuples).
2. Because they are immutable, you can’t really change tuples in place, but you can generate a new tuple with the desired value. Given T = (4, 5, 6), you can change the first item by making a new tuple from its parts by slicing and concatenating: T = (1,) + T[1:]. (Recall that single-item tuples require a trailing comma.) You could also convert the tuple to a list, change it in place, and convert it back to a tuple, but this is more expensive and is rarely required in practice—simply use a list if you know that the object will require in-place changes.
3. The default for the processing mode argument in a file open call is 'r', for reading text input. For input text files, simply pass in the external file’s name.
4. The pickle module can be used to store Python objects in a file without explicitly converting them to strings. The struct module is related, but it assumes the data is to be in packed binary format in the file; json similarly converts a limited set of Python objects to and from strings per the JSON format.
5. Import the copy module, and call copy.deepcopy(X) if you need to copy all parts of a nested structure X. This is also rarely seen in practice; references are usually the desired behavior, and shallow copies (e.g., aList[:], aDict.copy(), set(aSet)) usually suffice for most copies.
6. An object is considered true if it is either a nonzero number or a nonempty collection object. The built-in words True and False are essentially predefined to have the same meanings as integer 1 and 0, respectively.
7. Acceptable answers include “To learn Python,” “To move on to the next part of the book,” or “To seek the Holy Grail.”

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/87164297