Hướng dẫn how do you zip two unequal lists in python? - làm cách nào để nén hai danh sách không bằng nhau trong python?

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

>>> 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,)
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,)
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

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,)]

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,)]

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
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,)]

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,)]

5
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,)]

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,)]

5
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,)]

8
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
resultant 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,)]

3
resultant 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
resultant 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,)]

5
resultant 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,)]

5
resultant 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,)]

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,)]

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,)]

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
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,)]

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,)]

5
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,)]

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,)]

5
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,)]

8
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
resultant 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,)]

3
resultant 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
resultant 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,)]

5
resultant 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,)]

5
resultant 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_longest3itertools.zip_longest4 itertools.zip_longest5itertools.zip_longest6
Method #3: Using deque()

itertools.zip_longest7itertools.zip_longest8 itertools.zip_longest9zip_longest0 zip_longest1zip_longest2

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,)]

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,)]

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
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,)]

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,)]

5
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,)]

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,)]

5
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,)]

8
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
resultant 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,)]

3
resultant 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
resultant 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,)]

5
resultant 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_longest3itertools.zip_longest4 itertools.zip_longest5itertools.zip_longest6

itertools.zip_longest7itertools.zip_longest8 itertools.zip_longest9zip_longest0 zip_longest1zip_longest2

>>> 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,)
41

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,)]

5
resultant 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

>>> 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,)
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.
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,)
49
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,)]

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,)
52
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,)
54
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,)
56
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,)
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,)
64
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,)]

3
resultant 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,)
58
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,)
71
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}
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,)
78

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,)
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,)
85
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
>>> 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,)
87

itertools.zip_longest8

>>> 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_longest0 zip_longest1
>>> 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_longest4 itertools.zip_longest5
>>> 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,)
97

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
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,)]

03

Output:

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,)]

04

This 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,)
49
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,)]

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,)
52
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,)
54
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,)
56
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,)
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,)
64
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,)]

3
resultant 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,)
58
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,)
71
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}
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,)
78

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,)
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,)
85
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
>>> 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,)
87

itertools.zip_longest8

>>> 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_longest0 zip_longest1
>>> 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_longest4 itertools.zip_longest5
>>> 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,)
97

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
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,)]

03

Output:

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)]


Bạn có thể zip hai danh sách trong Python không?

Bạn cũng có thể zip hai danh sách trong Python với sự kết hợp của hàm zip () và cho vòng lặp. Đó là phương pháp ít được sử dụng nhất trong đó tập hợp xen kẽ có thể được thực hiện với hàm chuỗi, trong khi tập hợp nội bộ có thể được thực hiện với hàm zip ().. It is the least used method where the interlist aggregation can be done with the chain function, while the intralist aggregation can be done with the zip() function.

Tôi có thể sử dụng ZIP cho nhiều hơn hai danh sách Python không?

Hàm python zip () giúp bạn dễ dàng khóa hơn hai danh sách. Điều này hoạt động chính xác như bạn mong đợi, có nghĩa là bạn chỉ cần truyền trong danh sách dưới dạng các đối số khác nhau.. This works exactly like you'd expect, meaning you just only need to pass in the lists as different arguments.

Chúng ta có thể zip hai bộ dữ liệu trong Python không?

12.5 Danh sách và Tuples ZIP là một hàm tích hợp có hai hoặc nhiều chuỗi và các zips, chúng vào một danh sách các bộ dữ liệu trong đó mỗi bộ chứa một phần tử từ mỗi chuỗi.Trong Python 3, ZIP trả về một bộ lặp của các bộ dữ liệu, nhưng đối với hầu hết các mục đích, một trình lặp lại hoạt động như một danh sách.zip is a built-in function that takes two or more sequences and “zips” them into a list of tuples where each tuple contains one element from each sequence. In Python 3, zip returns an iterator of tuples, but for most purposes, an iterator behaves like a list.

Có nghĩa là gì trong Zip Python?

Trong Python, * là toán tử 'splat'.Nó được sử dụng để giải nén một danh sách vào các đối số.Ví dụ: foo (*[1, 2, 3]) giống như foo (1, 2, 3).Hàm zip () lấy n iterables và trả về các bộ dữ liệu y, trong đó y là độ dài ít nhất của tất cả các vòng lặp được cung cấp.the 'splat' operator. It is used for unpacking a list into arguments. For example: foo(*[1, 2, 3]) is the same as foo(1, 2, 3) . The zip() function takes n iterables, and returns y tuples, where y is the least of the length of all of the iterables provided.