-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzip.py
50 lines (30 loc) · 916 Bytes
/
zip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/env python
'''
Example: Interleave two (or more lists) with zip
Note: this is is not related to the Windows .zip file
'''
import itertools
# Interleave l1 and l2. lMerged will be a list of two-tuples.
l1 = [1, 2, 3]
l2 = [11, 12, 13]
lMerged = zip(l1, l2)
print('l1: %s' % l1)
print('l2: %s' % l2)
print('lMerged: %s' % lMerged)
# Show how to iterate over the merged list.
for a, b in lMerged:
print('a: %s b: %s' % (a, b))
# The same, but note that lMerged will be the length of the shortest list.
l1 = [1, 2, 3]
l2 = [11, 12, 13, 14, 15]
lMerged = zip(l1, l2)
print('l1: %s' % l1)
print('l2: %s' % l2)
print('lMerged: %s' % lMerged)
# Use itertools.izip_longest to grow the shorter list to the length of the longer list.
l1 = [1, 2, 3]
l2 = [11, 12, 13, 14, 15]
lMerged = itertools.izip_longest(l1, l2)
print('l1: %s' % l1)
print('l2: %s' % l2)
print('lMerged: %s' % list(lMerged))