Python里一些比较有意思的用法/Some funny usages in Python

记录一些日常遇到的感觉比较有意思的用法。

1. a[0:] means all elements of list a, while a[0:-1] means all but the last one elements.

2. a = np.zeros(2,4), then: a[:][0]=a[0][:]=[0,0,0,0], while a[:, 0]=[0,0]

This is because a[:][0] will be dealt in two steps: first, interpreter reads a[:] (and denotes it as b), we get zeros(2,4); second, interpreter reads b[0], we get [0,0,0,0]. Same as a[0][:]

3. unbound method get_angstrom2bohr() must be called with length instance as first argument (got nothing instead)

  This can be solved by add () after a class

4. 将字符串当作一个整体: b=['xxxx',]

 1 >>> a='xxxx'
 2 >>> for i in a: print i
 3 ... 
 4 x
 5 x
 6 x
 7 x
 8 >>> b=[a,]
 9 >>> for i in b: print i
10 ... 
11 xxxx

  

原文地址:https://www.cnblogs.com/zjyx/p/5979380.html