Python中不同大小列表的壓縮


簡介

在Python中,列表是儲存數值或字串值的常用方法之一。它們是可變的,並使用方括號[]定義。此類列表可以包含不同型別的元素,這些元素可以具有不同的資料型別。有時為了資料預處理的目的,我們可能需要壓縮Python中的不同列表。

在本文中,我們將討論列表的壓縮操作,以及如何使用不同的方法和技術壓縮Python中不同大小的列表。本文將幫助讀者理解列表的壓縮操作,並在需要時幫助讀者執行相同的操作。

現在讓我們開始討論列表及其壓縮操作。

列表的壓縮

眾所周知,列表是儲存元素的常用方法,其中可以包含數值或字串值。它們是可變型別,在使用Python處理資料集時經常使用。

列表的壓縮操作意味著我們實際上正在壓縮兩個不同的列表,或者更簡單地說,我們正在配對兩個不同列表的值。

為了闡明其背後的思想,讓我們舉個例子。假設我們有兩個列表

L1 = [1, 2, 3]

L2 = ['One', 'Two', 'Three']

如上所示,我們有兩個不同的列表,一旦我們對它們執行壓縮操作,輸出將是

Zipped_List = [(1, 'One'), (2, 'Two'), (3, 'Three')]

現在讓我們討論在Python中壓縮列表的用例。

壓縮列表的應用

壓縮大小相同或不同的兩個不同列表可能在許多情況下有所幫助。讓我們討論一下這些情況

字典表示: 對兩個不同列表的壓縮操作可以幫助我們建立或表示列表為字典。我們可以透過使用一個帶有鍵的列表和另一個帶有字典值的列表來做到這一點。

資料處理: 在某些情況下,為了繼續執行任務,必須進行資料處理,其中可能需要一個公共列表而不是許多不同的列表。壓縮操作在這種情況下可能非常有用。

資料迭代: 當您想要迭代列表元素並想要對它們執行某些操作時,也可以使用壓縮操作。

壓縮列表

有很多方法可以用來壓縮不同的列表,讓我們討論其中的一些。

方法1:使用帶列舉的for迴圈

使用帶列舉的for迴圈是壓縮兩個不同大小列表最簡單的方法之一。(此處應補充程式碼示例)

# Using For Loop with Enumerate
#1. Define two lists 2. Run a for loop with enumerate 3. Zip the lists

# define the two lists
list1 = [1,2,3,4,5,6]
list2 = [1, 5, 6]

# print the original lists
print ("The input list 1 is : " + str(list1))
print ("The input list 2 is : " + str(list2))

# for i, j run a for loop with enumerate
# append the values with j
res = []
for i, j in enumerate(list1):
  res.append((j, list2[i % len(list2)]))

# print the zipped list
print ("The Zip List from List 1 and 2 is : " + str(res))

正如我們在上面的程式碼中看到的,我們輸入了兩個不同大小的列表list1和list2。

首先,我們列印原始列表,然後執行帶有enumerate函式的for迴圈,該函式將附加列表元素並將兩個列表壓縮在一起。

輸出

以下程式碼的輸出將是(此處應補充輸出示例)

The input list 1 is : [1, 2, 3, 4, 5, 6] 
The input list 2 is : [1, 5, 6] 
The Zip List from List 1 and 2 is : [(1, 1), (2, 5), (3, 6), (4, 1), (5, 5), (6, 6)]

方法2:使用zip()方法

使用zip()關鍵字也可以幫助我們壓縮兩個不同大小的列表。在這裡,我們可以在迴圈中使用這個特定的關鍵字。(此處應補充程式碼示例)

# using Zip()

# define the list
num_list = [1, 2, 3, 4] # numerical list
str_list = ['one', 'two', 'three', 'four', 'none', 'none'] #string list

# zip the lists with using zip()

zipped_list = [(num, s) for num, s in zip(num_list, str_list)]
print(zipped_list)

正如我們在上面的程式碼中看到的,我們有兩個不同大小的列表,我們使用zip()來附加列表元素並壓縮列表。

輸出

以下程式碼的輸出將是(此處應補充輸出示例)

[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

方法3:使用itertools

這是壓縮兩個不同大小列表的經典方法之一。在這裡,我們將使用itertools來壓縮列表。(此處應補充程式碼示例)

# using the itertools 

# itertools + cycle

# import the cycle from itertools
from itertools import cycle

# define two different lists
list1 = [1,2,3,4,5,6,7]
list2 = [10, 50, 21]

# print the list1 and list2
print ("The input list 1 is : " + str(list1))
print ("The input list 2 is : " + str(list2))

# now use the cycle imported from itertools 
res = list(zip(list1, cycle(list2))
      if len(list1) > len(list2) #check for conditions
      else zip(cycle(list1), list2))

# printing the zipped list
print ("The Zip List from List 1 and 2 is: " + str(res))

正如我們在上面的程式碼中看到的,已經安裝了itertools庫,並且從其中匯入了cycle。

然後我們定義了兩個不同大小的列表並列印了它們。接下來,cycle用於透過將兩個列表都傳遞給它來壓縮列表。

輸出

這段程式碼的輸出將是(此處應補充輸出示例)

The input list 1 is : [1, 2, 3, 4, 5, 6, 7] 
The input list 2 is : [10, 50, 21] 
The Zip List from List 1 and 2 is : [(1, 10), (2, 50), (3, 21), (4, 10), (5, 50), (6, 21), (7, 10)]

結論

在本文中,我們討論了列表,什麼是列表的壓縮操作,它們的應用以及如何在Python中壓縮兩個不同大小的列表。

我們討論了三種方法,可以使用這些方法來壓縮Python中的列表,並且可以根據問題陳述和要求使用其中任何一種方法來壓縮列表。本文將幫助讀者理解列表的壓縮操作,並在需要時幫助讀者執行相同的操作。

更新於:2023年7月27日

422 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.