Thông thường, bạn sử dụng itertools.zip_longest
cho điều này:
>>> import itertools
>>> a = [1, 2, 3]
>>> b = [9, 10]
>>> for i in itertools.zip_longest[a, b]: print[i]
...
[1, 9]
[2, 10]
[3, None]
Nhưng zip_longest
miếng đệm ngắn hơn có thể lặp lại với
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
0 [hoặc bất kỳ giá trị nào bạn vượt qua dưới dạng tham số >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
1]. Nếu đó không phải là những gì bạn muốn thì bạn có thể sử dụng khả năng hiểu để lọc ra >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
0s:>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
Nhưng lưu ý rằng nếu một trong hai vòng lặp có giá trị ____10, điều này cũng sẽ lọc chúng ra. Nếu bạn không muốn điều đó, hãy xác định đối tượng của riêng bạn cho
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
1 và lọc thay vì >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
0:sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
Cải thiện bài viết
Lưu bài viết
Đưa ra hai danh sách có độ dài có thể không đồng đều, nhiệm vụ là zip hai danh sách trong từ điển sao cho danh sách có độ dài ngắn hơn sẽ tự lặp lại.
Vì từ điển trong Python là một bộ sưu tập khóa: các cặp giá trị không có thứ tự, kết quả sẽ được in theo kiểu không có thứ tự.
Phương pháp số 1: Sử dụng
6>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
7 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
8>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
9 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
0sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
1sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
4sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
6sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
8sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}0__25
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}2
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}4
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}7
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}9
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}1
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}3
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}3
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}5
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}7
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}8
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}1
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}4
Output:
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}
& nbsp; Phương pháp #2: Sử dụng sự hiểu biết của Dict
Method #2: Using dict comprehension
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
7 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
8>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
9 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
0sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
1sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
4sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
6sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
8sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}0__25
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}2
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}4
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}7
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}9
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}1
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}3
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}3
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}5
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}7
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}8
& nbsp; Phương pháp #2: Sử dụng sự hiểu biết của Dict
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}1
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}4
Output:
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}3
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 itertools.zip_longest
3itertools.zip_longest
4 itertools.zip_longest
5itertools.zip_longest
6Method #3: Using deque[]
itertools.zip_longest
7itertools.zip_longest
8 itertools.zip_longest
9zip_longest
0 zip_longest
1zip_longest
2
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
1sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
4sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
6sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
8sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}0__25
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}2
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}4
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}7
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}9
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}1
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}3
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}3
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}5
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}7
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}8
& nbsp; Phương pháp #2: Sử dụng sự hiểu biết của Dict
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}3
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 itertools.zip_longest
3itertools.zip_longest
4 itertools.zip_longest
5itertools.zip_longest
6itertools.zip_longest
7itertools.zip_longest
8 itertools.zip_longest
9zip_longest
0 zip_longest
1zip_longest
2
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
32>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
41resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}1
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}4
Output:
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}
Trong Python, Zipping là một tiện ích nơi chúng tôi ghép một danh sách với danh sách kia. Thông thường, nhiệm vụ này chỉ thành công trong các trường hợp khi kích thước của cả hai danh sách được nén có cùng kích thước. Nhưng đôi khi chúng tôi yêu cầu rằng các danh sách có kích thước khác nhau cũng phải được nén. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết nếu nó xảy ra.
Phương pháp số 1: Sử dụng
48 + loopthis là cách chúng ta sử dụng phương pháp vũ lực để đạt được nhiệm vụ cụ thể này. Trong quá trình này, chúng tôi lặp lại cả danh sách và khi một người trở nên lớn hơn các yếu tố khác, chúng tôi chu kỳ các yếu tố để bắt đầu chúng ngay từ đầu.>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
This is the way in which we use the brute force method to achieve this particular task. In
this process, we loop both the list and when one becomes larger than other we cycle the elements to begin them from the beginning.
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
49sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
52sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
54sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
56sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
58__>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
64sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}7
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
58sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
71resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}3
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
75 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
76 resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
78resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
81 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
76 resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
84>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
85sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
87itertools.zip_longest
8
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
89zip_longest
0 zip_longest
1>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
92>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
32>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
94itertools.zip_longest
4 itertools.zip_longest
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
97resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
00 ________ 176 & nbsp; resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
03Output:
The original list 1 is : [7, 8, 4, 5, 9, 10] The original list 2 is : [1, 5, 6] The zipped list is : [[7, 1], [8, 5], [4, 6], [5, 1], [9, 5], [10, 6]]
& nbsp; Phương pháp số 2: Sử dụng
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
04 này là một cách khác để thực hiện nhiệm vụ cụ thể này, trong đó, chúng tôi chu kỳ danh sách nhỏ hơn để nó có thể bắt đầu zipping từ đầu trong trường hợp danh sách nhỏ hơn bị cạn kiệt bằng hàm zip.Method #2 : Using
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
04This is yet another way to perform this particular task, in this we cycle the smaller list so that it can begin zipping from beginning in case the smaller list gets exhausted using a zip function.
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
7 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
8>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
9 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
0>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
49sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
52sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
54sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
56sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
58__>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
64sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
3resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}7
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
58sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
71resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}3
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
75 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
76 resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
78resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
81 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
76 resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
84>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
85sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
2 >>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
87itertools.zip_longest
8
>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
89zip_longest
0 zip_longest
1>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
92>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
32>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
94itertools.zip_longest
4 itertools.zip_longest
5>>> for i in [tuple[p for p in pair if p is not None]
... for pair in itertools.zip_longest[a, b]]:
... print[i]
...
[1, 9]
[2, 10]
[3,]
97resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}9
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}6
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
00 ________ 176 & nbsp; resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}3
sentinel = object[]
def zip_longest_no_fill[a, b]:
for i in itertools.zip_longest[a, b, fillvalue=sentinel]:
yield tuple[x for x in i if x is not sentinel]
list[zip_longest_no_fill[a, b]] # [[1, 9], [2, 10], [3,]]
03Output:
The original list 1 is : [7, 8, 4, 5, 9, 10] The original list 2 is : [1, 5, 6] The zipped list is : [[7, 1], [8, 5], [4, 6], [5, 1], [9, 5], [10, 6]]