python 答疑

问:

Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> s=”01234″
>>> s[0:1]
‘0’
>>>
字符串[:] 这种形式,也是左闭右开的区间吗?
所以
range(x,y) = [x,y] 等于数学上的 [ x, y)
答:
一般而言,无论是在python里还是其它的编程语言,range一般都是左闭右开。
不局限地python中的s[x:y],range(x,y)
像c++ STL中的begin(),end()等都是这样,end()-1才是最后一个元素。“
问:
python import copy中有 shallow copy 和 deep (recursive) copy
import copy

newobj = copy.copy(oldobj) # shallow copy
newobj = copy.deepcopy(oldobj) # deep (recursive) copy

对于dict={“string”:”string”, …} 这样的dict
shallow copy和deep (recursive) copy的结果是相同的, 都cop

Leave a Reply

Your email address will not be published. Required fields are marked *