20180213 字符串spilt方法,字符串打包zip方法

  • 切割字符串 split('x')

>>> str = "http://baidu.com/test123?a=ddd&b=hhh"
>>> s1 = str.split('?')                            #切割字符串后返回,切分为2部分的字符串列表
>>> print(s1)
['http://baidu.com/test123', 'a=ddd&b=hhh']
>>> s2 = s1[1].split('&')
>>> print(s2)
['a=ddd', 'b=hhh']
>>> print(s2[1])
b=hhh

  • zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表

>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c) # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped) # 与 zip 相反,可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]

原文地址:https://www.cnblogs.com/hazelrunner/p/8444965.html