# encoding: utf-8
""" return 返回的是完整的结果
yield 返回的是一个值"""
def myzip1(*seqs):
"""使用返回值"""
seqs = [list(S) for S in seqs]
res = []
while all(seqs):
res.append(tuple(x.pop(0) for x in seqs))
return res
def myzip2(*seqs):
"""使用生成器"""
seqs = [list(S) for S in seqs]
# print(seqs)
while all(seqs):
yield tuple(x.pop(0) for x in seqs)
def myzip3(*seqs, pad=None):
"""使用返回值"""
seqs = [list(S) for S in seqs]
# print(seqs)
res = []
while any(seqs):
res.append(tuple((x.pop(0) if x else pad) for x in seqs))
return res
def myzip4(*seqs, pad=None):
"""使用生成器"""
seqs = [list(S) for S in seqs]
# print(seqs)
while any(seqs):
yield tuple((x.pop(0) if x else pad) for x in seqs)
def myzip5(*seqs):
"""使用计数器进行限制"""
minlen = min([len(x) for x in seqs])
return [tuple(S[i] for S in seqs) for i in range(minlen)]
def myzip6(*seqs, pad=None):
"""使用计数器进行限制"""
maxlen = max(len(x) for x in seqs)
return [tuple((S[i] if i < len(S) else pad) for S in seqs) for i in range(maxlen)]
# myzip1test = True
myzip1test = False
if __name__ == '__main__' and myzip1test:
S1, S2 = 'abc', 'xyz123'
print(myzip1(S1, S2))
# myzip2test = True
myzip2test = False
if __name__ == '__main__' and myzip2test:
S1, S2 = 'abc', 'xyz123'
print(list(myzip2(S1, S2)))
# myzip3test = True
myzip3test = False
if __name__ == '__main__' and myzip3test:
S1, S2 = 'abc', 'xyz123'
print(myzip3(S1, S2, pad=10))
# myzip4test = True
myzip4test = False
if __name__ == '__main__' and myzip4test:
S1, S2 = 'abc', 'xyz123'
print(list(myzip4(S1, S2, pad=10)))
# myzip5test = True
myzip5test = False
if __name__ == '__main__' and myzip5test:
S1, S2 = 'abc', 'xyz123'
print(myzip5(S1, S2))
# myzip6test = True
myzip6test = False
if __name__ == '__main__' and myzip6test:
S1, S2 = 'abce', 'xyz123'
print(myzip6(S1, S2))