Phương thức python zip [] lấy ITEBLE hoặc các thùng chứa và trả về một đối tượng lặp duy nhất, có các giá trị được ánh xạ từ tất cả & nbsp; container. & Nbsp; takes iterable or containers and returns a single iterator object, having mapped values from all the containers.
Nó được sử dụng TomAP Chỉ số tương tự của nhiều container để chúng có thể được sử dụng chỉ bằng cách sử dụng một thực thể duy nhất. & NBSP;map the similar index of multiple containers so that they can be used just using a single entity.
Cú pháp: & nbsp; zip [*iterators] & nbsp; zip[*iterators]
Các tham số: Python Iterables hoặc Container [Danh sách, Chuỗi, v.v.] & NBSP; Giá trị trả về: & nbsp; Trả về một đối tượng iterator duy nhất, có các giá trị được ánh xạ từ tất cả & nbsp; container.Python iterables or containers [ list, string etc ]
Return Value : Returns a single iterator object, having mapped values from all the
containers.
Ví dụ về Python Zip []
Ví dụ 1: Python Zip Two Danh sách
Python3
name
=
[
"Manjeet"
,
0 Mukesh 24 1 Roni 50 2 Chari 180____9
0 Mukesh 24 1 Roni 50 2 Chari 182
0 Mukesh 24 1 Roni 50 2 Chari 186
=
[
0 Mukesh 24 1 Roni 50 2 Chari 189
,
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}1
,
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}3
,
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}5
0 Mukesh 24 1 Roni 50 2 Chari 185
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}7
=
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}9
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]0
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]3
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]4
Output:
{['Shambhavi', 3], ['Nikhil', 1], ['Astha', 2], ['Manjeet', 4]}
Ví dụ 2: Python Zip liệt kêenumerate
Python3
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]5
=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]8
,
Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 430
,
Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 432
0 Mukesh 24 1 Roni 50 2 Chari 185
Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 434
=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 437
,
Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 439
,
name
10 Mukesh 24 1 Roni 50 2 Chari 185
name
3 name
4name
5 name
6
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}9
name
9=
0
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
=
2Output:
0 Mukesh 24 1 Roni 50 2 Chari 18
Ví dụ 3: Từ điển Python Zip []
Python3
=
3=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
=
6,
=
8,
[
00 Mukesh 24 1 Roni 50 2 Chari 185
[
2=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
[
5,
[
779__[
90 Mukesh 24 1 Roni 50 2 Chari 185
"Manjeet"
1=
"Manjeet"
3name
3 "Manjeet"
5
"Manjeet"
6[
2name
5
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}9
,
0The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
,
2Output:
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}
Làm thế nào để giải nén? & Nbsp;
Giải nén có nghĩa là chuyển đổi các giá trị có khóa kéo trở lại bản thân cá nhân. Điều này được thực hiện với sự trợ giúp của toán tử**.*” operator.
Python3
name
=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
"Manjeet"
,
0 Mukesh 24 1 Roni 50 2 Chari 180____9
0 Mukesh 24 1 Roni 50 2 Chari 182
0 Mukesh 24 1 Roni 50 2 Chari 186
=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
0 Mukesh 24 1 Roni 50 2 Chari 189
,
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}1
,
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}3
,
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}5
0 Mukesh 24 1 Roni 50 2 Chari 185
0 Mukesh 24 1 Roni 50 2 Chari 1815
=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
0 Mukesh 24 1 Roni 50 2 Chari 1818
,
Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 439
,
__1222222124{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}7
=
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}9
0 Mukesh 24 1 Roni 50 2 Chari 1829
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}7
=
0 Mukesh 24 1 Roni 50 2 Chari 1832
0 Mukesh 24 1 Roni 50 2 Chari 1833
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
0 Mukesh 24 1 Roni 50 2 Chari 1836
0 Mukesh 24 1 Roni 50 2 Chari 1837
=
0 Mukesh 24 1 Roni 50 2 Chari 1839
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
0 Mukesh 24 1 Roni 50 2 Chari 1833
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
0 Mukesh 24 1 Roni 50 2 Chari 1844
0 Mukesh 24 1 Roni 50 2 Chari 1845
0 Mukesh 24 1 Roni 50 2 Chari 1846
=
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}9
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
0 Mukesh 24 1 Roni 50 2 Chari 1850
0 Mukesh 24 1 Roni 50 2 Chari 1851
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
0 Mukesh 24 1 Roni 50 2 Chari 1854
0 Mukesh 24 1 Roni 50 2 Chari 1837
=
0 Mukesh 24 1 Roni 50 2 Chari 1839
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
0 Mukesh 24 1 Roni 50 2 Chari 1860
0 Mukesh 24 1 Roni 50 2 Chari 1837
=
0 Mukesh 24 1 Roni 50 2 Chari 1839
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
0 Mukesh 24 1 Roni 50 2 Chari 1865
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
0 Mukesh 24 1 Roni 50 2 Chari 1868
0 Mukesh 24 1 Roni 50 2 Chari 1837
=
0 Mukesh 24 1 Roni 50 2 Chari 1839
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
0 Mukesh 24 1 Roni 50 2 Chari 1873
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
0 Mukesh 24 1 Roni 50 2 Chari 1876
0 Mukesh 24 1 Roni 50 2 Chari 1837
=
0 Mukesh 24 1 Roni 50 2 Chari 1839
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
0 Mukesh 24 1 Roni 50 2 Chari 1881
Output:
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]
Ứng dụng thực tế
Có nhiều ứng dụng có thể được cho là được thực thi bằng ZIP, có thể là cơ sở dữ liệu sinh viên hoặc thẻ điểm hoặc bất kỳ tiện ích nào khác yêu cầu ánh xạ các nhóm. Một ví dụ nhỏ về thẻ điểm được thể hiện dưới đây. & NBSP;student database or scorecard or any other utility that requires mapping of groups. A small example of scorecard is demonstrated below.
Python3
0 Mukesh 24 1 Roni 50 2 Chari 1882
=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
0 Mukesh 24 1 Roni 50 2 Chari 1885
,
0 Mukesh 24 1 Roni 50 2 Chari 1887
,
0 Mukesh 24 1 Roni 50 2 Chari 1889
,
__19190 Mukesh 24 1 Roni 50 2 Chari 1895
=
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]7
0 Mukesh 24 1 Roni 50 2 Chari 1898
,
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}00
,
__202name
3
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}09
name
5 {'reliance': 2175, 'infosys': 1127, 'tcs': 2750}9
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}12
=
0
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]1
The zipped result is : [['Manjeet', 4, 40], ['Nikhil', 1, 50], ['Shambhavi', 3, 60], ['Astha', 2, 70]] The unzipped result: The name list is : ['Manjeet', 'Nikhil', 'Shambhavi', 'Astha'] The roll_no list is : [4, 1, 3, 2] The marks list is : [40, 50, 60, 70]2
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}16
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}17
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}18
Output:
Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 43