Hướng dẫn when to use zip python - khi nào nên sử dụng zip python
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. Show
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.
Ví dụ về Python Zip ()Ví dụ 1: Python Zip Two Danh sáchPython3
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êenumeratePython3The 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
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
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
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
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
{'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
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. Python30 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 , __202
{'reliance': 2175, 'infosys': 1127, 'tcs': 2750}09 name 5 {'reliance': 2175, 'infosys': 1127, 'tcs': 2750}9 {'reliance': 2175, 'infosys': 1127, 'tcs': 2750}12
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 Zip có phải là Python nhanh hơn không?Không, nó không nhanh hơn.Chỉ viết dường như nhanh hơn in.Giải pháp của bạn dường như là về bản đồ thay vì zip, không viết thay vì in.. Only write seems to be faster than print . Your solution seemed to be about map instead of zip , not write instead of print .
Tôi có thể sử dụng gì thay vì zip trong Python?Đó là itertools.izip (): Tạo một trình lặp tập hợp các phần tử từ mỗi lần lặp.Giống như zip () ngoại trừ việc nó trả về một trình lặp thay vì một danh sách.itertools. izip() : Make an iterator that aggregates elements from each of the iterables. Like zip() except that it returns an iterator instead of a list.
Sự khác biệt giữa liệt kê và zip trong Python là gì?Hàm liệt kê () trả về các chỉ mục của tất cả các mục trong Iterables (danh sách, từ điển, bộ, v.v.) trong khi hàm zip () được sử dụng để tổng hợp hoặc kết hợp nhiều lần lặp. whereas the zip() function is used to aggregate, or combine, multiple iterables.
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. |