Python黑魔法

1. 赋值

In [1]: x = 1
   ...: y = 21
   ...: print x, y
   ...: 
   ...: x, y = y, x
   ...: print x, y
1 21
21 1

2. 列表合并

In [2]: a1 = [(2,3),(3,4)]
    ...: a2 = [(4,5)]
    ...: a = a1 + a2
    ...: print a
[(2, 3), (3, 4), (4, 5)]

3. 字典合并

方式1:

In [3]: d1 = {'a': 1}
    ...: d2 = {'b': 2}
    ...: 
    ...: d1.update(d2)
    ...: print d1
{'a': 1, 'b': 2}

方式2:

In [4]: d1 = {'a': 1}
    ...: d2 = {'b': 2}
    ...: 
    ...: d = dict(d1.items() + d2.items())
    ...: print d
{'a': 1, 'b': 2}

方式3:

In [5]: d1 = {'a': 1}
    ...: d2 = {'b': 2}
    ...: 
    ...: d = dict(d1, **d2)  # 内置函数dict(iterable, **kwarg)
    ...: print d
{'a': 1, 'b': 2}

 

 

待续

 

posted @ 2016-11-15 22:02  Zhao-Pace  阅读(371)  评论(0编辑  收藏  举报