Kinh Nghiệm về Python add two lists different length Mới Nhất
Bạn đang tìm kiếm từ khóa Python add two lists different length được Cập Nhật vào lúc : 2022-01-23 21:52:15 . Với phương châm chia sẻ Bí quyết về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Ad lý giải và hướng dẫn lại nha.
Often you might be interested in zipping (or merging) together two lists in Python. Fortunately this is easy to do using the zip() function.
Nội dung chính
- Example 1: Zip Two Lists of Equal Length into One ListExample 2: Zip Two Lists of Equal Length into a DictionaryExample 3: Zip Two Lists of Unequal LengthVideo liên quan
This tutorial shows several examples of how to use this function in practice.
Example 1: Zip Two Lists of Equal Length into One List
The following syntax shows how to zip together two lists of equal length into one list:
#define list a and list b
a = [‘a’, ‘b’, ‘c’]
b = [1, 2, 3]
#zip the two lists together into one list
list(zip(a, b))
[(‘a’, 1), (‘b’, 2), (‘c’, 3)]
Example 2: Zip Two Lists of Equal Length into a Dictionary
The following syntax shows how to zip together two lists of equal length into a dictionary:
#define list of keys and list of values
keys = [‘a’, ‘b’, ‘c’]
values = [1, 2, 3]
#zip the two lists together into one dictionary
dict(zip(keys, values))
‘a’: 1, ‘b’: 2, ‘c’: 3
Example 3: Zip Two Lists of Unequal Length
If your two lists have unequal length, zip() will truncate to the length of the shortest list:
#define list a and list b
a = [‘a’, ‘b’, ‘c’, ‘d’]
b = [1, 2, 3]
#zip the two lists together into one list
list(zip(a, b))
[(‘a’, 1), (‘b’, 2), (‘c’, 3)]
If youd like to prevent zip() from truncating to the length of the shortest list, you can instead use the zip_longest() function from the itertools library.
By default, this function fills in a value of None for missing values:
from itertools import zip_longest
#define list a and list b
a = [‘a’, ‘b’, ‘c’, ‘d’]
b = [1, 2, 3]
#zip the two lists together without truncating to length of shortest list
list(zip_longest(a, b))
[(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, None)]
However, you can use thefillvalueargument to specify a different fill value to use:
#define list a and list b
a = [‘a’, ‘b’, ‘c’, ‘d’]
b = [1, 2, 3]
#zip the two lists together, using fill value of ‘0’
list(zip_longest(a, b, fillvalue=0))
[(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 0)]
You can find the complete documentation for the zip_longest() function here.
Reply
0
0
Chia sẻ
Review Python add two lists different length ?
Bạn vừa Read tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Clip Python add two lists different length tiên tiến và phát triển nhất
Share Link Down Python add two lists different length miễn phí
You đang tìm một số trong những Chia SẻLink Download Python add two lists different length miễn phí.
Thảo Luận vướng mắc về Python add two lists different length
Nếu You sau khi đọc nội dung bài viết Python add two lists different length , bạn vẫn chưa hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Ad lý giải và hướng dẫn lại nha
#Python #add #lists #length