【Python】小技巧's

目录:

1、List等分切割
2、三元运算符+多元赋值
3、字典的引用、浅拷贝、深拷贝

1、List等分切割

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> s = [1,2,3,4,5,6,7,8,9,0]
>>> t = []
>>> for i in range(0,len(s),4):
    t.append(s[i:i+4])

    
>>> t
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0]]
>>> 

这样就可以优雅的切割List了!
当然还可以更优雅t = [s[i:i+4] for i in range(0,len(s),4)]

2、三元运算符+多元赋值

使用Python模拟三元运算符:

result = '正' if b > 0 else '负' #如果b大于0则赋值result“正”,否则赋值“负”

多元赋值:

a,b = 1,2 #简化多元赋值
a,b = (1,2) #标准多元赋值
普通多元赋值的时候可以省略括号,但实际上赋值时是元组方式

多元赋值 + 三元运算符:

a,b = (1,2) if b>0 else (5,6) #如果多元赋值与模拟三元运算结合,则必须添加括号

案例:

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> s = 3
>>> a,b = 1,2 if s<=3 else 5,6 #前后均未添加括号

Traceback (most recent call last):
  File "", line 1, in 
    a,b = 1,2 if s<=3 else 5,6
ValueError: too many values to unpack
>>> a,b = 1,2 if s<=3 else 5
>>> a
1
>>> b
2
>>> a,b = 1,2 if s>3 else 5 #前面未加括号,后面只有一个值时比较运算只对b有效
>>> a
1
>>> b
5
>>> a,b = 1,2 if s>3 else (5,6) #前面未加括号,后面加括号时,比较运算只对b有效且元组中的值全部赋给b
>>> a
1
>>> b
(5, 6)
>>> a,b = (1,2) if s>3 else (5,6) #前后都添加括号则正确
>>> a
5
>>> b
6

3、字典的引用、浅拷贝、深拷贝

最近遇到点小疑问,关于Python字典的拷贝,在IDE中验证了一下,环境及过程如下

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> d = {"test":"test1"}
>>> d1 = d.copy()
>>> d == d1
True
>>> id(d) == id(d1)
False
>>> id(d)
44954760L
>>> id(d1)
44954488L
>>> d['test']='123'
>>> d
{'test': '123'}
>>> d1
{'test': 'test1'}
>>> d2 = {"test":[{"name":"python"},]}
>>> d3 = d2.copy()
>>> d2
{'test': [{'name': 'python'}]}
>>> d3
{'test': [{'name': 'python'}]}
>>> id(d2)
44295848L
>>> id(d3)
44957768L
>>> d2['test'][0]['name'] = 'JavaScript'
>>> d2
{'test': [{'name': 'JavaScript'}]}
>>> d3
{'test': [{'name': 'JavaScript'}]}
>>> d4 = d2
>>> d4
{'test': [{'name': 'JavaScript'}]}
>>> d2['test'][0]['name'] = 'c++'
>>> d2
{'test': [{'name': 'c++'}]}
>>> d3
{'test': [{'name': 'c++'}]}
>>> d4
{'test': [{'name': 'c++'}]}
>>> d2['t']='testsssss'
>>> d2
{'test': [{'name': 'c++'}], 't': 'testsssss'}
>>> d3
{'test': [{'name': 'c++'}]}
>>> d4
{'test': [{'name': 'c++'}], 't': 'testsssss'}
>>> 
>>> import copy
>>> d5 = copy.deepcopy(d2)
>>> d5
{'test': [{'name': 'c++'}], 't': 'testsssss'}
>>> d2['t'] = '000000'
>>> d2
{'test': [{'name': 'c++'}], 't': '000000'}
>>> d3
{'test': [{'name': 'c++'}]}
>>> d4
{'test': [{'name': 'c++'}], 't': '000000'}
>>> d5
{'test': [{'name': 'c++'}], 't': 'testsssss'}
>>> d2['test'][0]['name'] = 'C'
>>> d2
{'test': [{'name': 'C'}], 't': '000000'}
>>> d3
{'test': [{'name': 'C'}]}
>>> d4
{'test': [{'name': 'C'}], 't': '000000'}
>>> d5
{'test': [{'name': 'c++'}], 't': 'testsssss'}
>>> 

如上,可以看出,
使用"="号进行赋值的属于引用,算不上拷贝了,新变量的值随着原数据的数据变化而变化。
使用copy函数为浅拷贝,字典的第一层被拷贝,但是更深层的数据还是引用方式传递,修改原来的数据还会影响深层的数据。
使用deepcopy函数为深拷贝,字典的所有值都被重新复制了一遍,原数据的操作不对拷贝之后的数据有任何影响。

...持续更新...

你可能感兴趣的:(【Python】小技巧's)