学习更多,欢迎关注微信公众号:Excel办公小技巧
1. 用法一:实现zip的逆操作
he=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
>>> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
zip(he)
>>> [([1, 2, 3, 4],), ([5, 6, 7, 8],), ([9, 10, 11, 12],)]
zip(*he)
>>> [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
zip(*zip(he))
>>> [([1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12])]
2. 用法二:星号表达式-代替多个元素
>>> cc
[1, 3, 4, 5, 7]
>>> a,*b,c
(1, 3, 4, 5, 7)
>>> b
[3, 4, 5]
3. 用法三:数学计算
def hah(*args,**kwargs):
for i in args:
print '{0}的3次幂={1}'.format(i, i ** 3)
print '{0}乘以3={1}'.format(i, i * 3)
print '把接收的参数作为元组压缩: ', zip(args)
print '把接收的参数作为元组解压: ', zip(*zip(args))
print '把接收的参数作为字典打印: ',kwargs
hah(2,4,6,pd='pandas',np='numpy',pm='pymongo')
输出:
2的3次幂=8
2乘以3=6
4的3次幂=64
4乘以3=12
6的3次幂=216
6乘以3=18
把接收的参数作为元组压缩: [(2,), (4,), (6,)]
把接收的参数作为元组解压: [(2, 4, 6)]
把接收的参数作为字典打印: {'np': 'numpy', 'pd': 'pandas', 'pm': 'pymongo'}