Python Tips, Sept-Oct, 17

待解决:工厂函数?生成器?闭包?类属性?动态语言?强弱类型?

1.3.x 里面,两个int除法返回一个float,2.x里面是正常情况。

2.深拷贝是完全创造了一个新的对象,浅拷贝创造了一个新对象,但是新对象里面的子对象还是指向原来子对象。http://blog.csdn.net/u014647208/article/details/77683545
3.set和dict的唯一区别仅在于没有存储对应的value,但是,set的原理和dict一样,所以,同样不可以放入可变对象,因为无法判断两个可变对象是否相等,也就无法保证set内部“不会有重复元素”。而且set和dict的key的子元素(子元素的子元素的子元素的子元素的……也不行,即化简之后的key必须为不可变元素)也必须为不可变的
4.pass可以啥也不做,可以加在任何地方:pass还可以用在其他语句里,比如:
if age >= 18:
pass
缺少了pass,代码运行就会有语法错误。
5.bar = [] if bar is None else bar 三元表达式,可以用于函数设置默认参数,注意,函数的默认参数在函数声明的时候就被初始化了。

6.关于声明函数的默认参数,可以参见如下代码:

INPUT:
def test(a=[]):
    a.append(":-)")
    return a

q=test()
print("test函数第一次在没有给定参数被调用时",q,id(q))
w=test([1])
e=test()
r=test([2])
t=test()

print("test函数在没有给定参数被调用三次后",q,id(q),'\n',w,id(w),'\n',e,id(e),'\n',r,id(r),'\n',t,id(t),'\n')

OUTPUT:
test函数第一次在没有给定参数被调用时 [':-)'] 54701312
test函数在没有给定参数被调用三次后 [':-)', ':-)', ':-)'] 54701312
 [1, ':-)'] 54727024
 [':-)', ':-)', ':-)'] 54701312
 [2, ':-)'] 54727344
 [':-)', ':-)', ':-)'] 54701312

7.print(None or [])--->[]               print([] or [1])--->[1]

8.可以通过
def test(a,c):
    print(a,c)

cav={'a':1,'c':2}
test(**cav)
def test(a,c):
    print(a,c)

cav=[1,2]
test(*cav)

这样来传递参数

9.如果要对list实现类似Java那样的下标循环怎么办?Python内置的enumerate函数可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身:
>>> for i, value in enumerate(['A', 'B', 'C']):
...     print(i, value)
...
0 A
1 B
2 C
10.Iterator&Iterable:
An iterable is an object that has an __iter__ method which returns an iterator, or which defines a __getitem__ method that can take sequential indexes starting from zero (and raises an IndexError when the indexes are no longer valid). So an iterable is an object that you can get an iterator from.
An iterator is an object with a next (Python 2) or __next__ (Python 3) method.
Whenever you use a for loop, or map, or a list comprehension, etc. in Python, the next method is called automatically to get each item from the iterator, thus going through the process of iteration.

11.reduce返回计算结果,map,filter返回一个iterator

12.a=1,2,返回a是一个tuple,list(iterable),list可以接受一个iterable而不能接收单个的字符或者数字参数,比如list((1,2)),可以返回一个list,但是list(1)不行,list(1,2)也不行。More detail:https://docs.python.org/3/library/stdtypes.html#list.
The constructor builds a list whose items are the same and in the same order as iterable’s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, the constructor creates a new empty list, [].

13.PYTHON居然可以这么玩:+1-2-+22--1++2----1
输出:-21,甚至还能这么玩:1-+-+-+1,输出是0,有点不清楚为啥可以这样= =

14.Timeit用来计时,具体用法自己查。

15.python数据结构的复杂度:https://wiki.python.org/moin/TimeComplexity

16.dict.setdefault(key, default=None):如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值。
dict.setdefault(key, default=None)如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值。

17.map(func,param1,param2...),后面只有一个参数时,func作用于参数的每一项,即以param1的每一项为func的参数进行运算;有两个或以上参数时,func同时接受所有参数相同index位置的值为func的参数,进行运算。

18.lambda表达式可以这么用:print( (lambda a,b:a+b)(1,2) )
输出为3

评论

此博客中的热门博文

225 Implement Stack using Queues

232. Implement Queue using Stacks

20. Valid Parentheses