Python 中如何壓縮不均勻元組
簡介
在 Python 中,元組是根據需求儲存和處理資料的一種廣泛使用的方法。元組中涉及許多操作,其中資料會根據問題陳述的要求進行預處理和轉換。壓縮操作是壓縮不同元組的最常見和最廣泛使用的操作之一。
在本文中,我們將討論 Python 中不均勻元組的壓縮,不均勻元組的壓縮到底意味著什麼,以及使用程式碼解釋執行相同操作的不同方法。本文將幫助人們瞭解不均勻元組壓縮背後的核心思想,並在需要時幫助人們執行相同的操作。
所以現在讓我們從討論 Python 中壓縮的含義和 Python 中不均勻元組的壓縮開始。
什麼是壓縮不均勻元組?
在 Python 中,“zip” 或“壓縮”表示我們將不同元組的元素加起來,這意味著我們將不同元組的元素配對並將其儲存在一個公共元組中。
例如,如果我們有兩個這樣的元組
T1 = (1, 2, 3)
T2 = (“one”, “two”, “three”)
那麼對這些元組進行壓縮操作將給出以下輸出
T_Zip = ((1, “one”), (2, “two”), (3, “three”))
這裡,不均勻元組表示兩個元組的大小或長度不相等,這意味著其中一個元組的大小比另一個元組小或大。對於大小或長度相同的元組,壓縮操作非常容易,但當涉及到壓縮兩個不同大小或不均勻的元組時,它會變得非常複雜。
但是,有一些方法可以用來壓縮兩個不均勻的元組。讓我們逐一討論一下。
壓縮不均勻元組
主要有三種方法可以用來壓縮 Python 中的不均勻元組。
使用 For 迴圈和列舉
使用列表推導式
使用 Numpy 庫
方法 1:使用 For 迴圈和列舉
我們可以使用 for 迴圈和 enumerate 函式壓縮不均勻元組。這是執行此操作的最簡單有效的方法之一。
# using for loop and enumerate # define the tuples test_tup1 = (7, 8, 4, 5) test_tup2 = (1, 5, 6) # print the input tuples print("The input tuple 1 is : " + str(test_tup1)) print("The input tuple 2 is : " + str(test_tup2)) res = [] # use for loop with enumerate for i, j in enumerate(test_tup1): res.append((j, test_tup2[i % len(test_tup2)])) # Print the final resultant tuple after zipping tuple 1 and 2 print("The output zipped tuple from tuple 1 and 2 is : " + str(res))
正如我們在上面的程式碼中看到的,元組 1 和 2 用 () 定義,它們的大小或長度不同。
現在,for 迴圈與 enumerate 一起使用,它追加元組 1 和元組 2 的元素,並以元組格式給出輸出。
輸出
以下程式碼的輸出將是
The input tuple 1 is : (7, 8, 4, 5) The input tuple 2 is : (1, 5, 6) The output zipped tuple from tuple 1 and 2 is : [(7, 1), (8, 5), (4, 6), (5, 1)]
方法 2:使用列表推導式
可以使用列表推導式壓縮兩個不均勻的元組。這裡可以使用三元運算子。
# using list comprehension # define the tuples tup1 = (7, 8, 4, 5) tup2 = (1, 5, 6) # print the input tuples print("The input tuple 1 is : " + str(tup1)) print("The input tuple 2 is : " + str(tup2)) # define if else conditions res = [(tup1[i], tup2[i % len(tup2)]) if len(tup1) > len(tup2) else (tup1[i % len(tup1)], tup2[i]) # use for loop on tuple 1 and 2 for i in range(max(len(tup1), len(tup2)))] #Print the final resultant tuple after zipping tuple 1 and 2 print(" The output zipped tuple from tuple 1 and 2 is :" + str(res))
正如我們在上面的程式碼中看到的,定義了兩個不同大小的元組,然後編寫了 if else 條件,其中首先檢查元組的長度,然後最終的 for 迴圈追加兩個元組並返回輸出。
輸出
以下程式碼的輸出將是
The input tuple 1 is : (7, 8, 4, 5) The input tuple 2 is : (1, 5, 6) The output zipped tuple from tuple 1 and 2 is : [(7, 1), (8, 5), (4, 6), (5, 1)]
方法 3:使用 Numpy 庫
Numpy 是用於對資料執行操作的最廣泛使用的庫之一。這裡使用陣列格式的資料,我們可以使用 numpy 做幾乎任何事情並將資料轉換為任何內容。
#using numpy module to zip the uneven tuples # Importing the numpy module import numpy as np # define the tuples test_tup1 = (7, 8, 4, 5) test_tup2 = (1, 5, 6) # convert the tuples into array format arr1 = np.array(test_tup1) arr2 = np.array(test_tup2) # use np.tile arr2_tiled = np.tile(arr2, (len(arr1) // len(arr2) + 1))[:len(arr1)] #use column_stack on array 1 and tiled array 2 to zip the tuples res_arr = np.column_stack((arr1, arr2_tiled)) # convert the array output to the tuple res = tuple(map(tuple, res_arr)) # Print the final resultant tuple after zipping tuple 1 and 2 print("The output zipped tuple from tuple 1 and 2 is : " + str(res))
正如我們在上面的程式碼中看到的,我們首先匯入了 numpy 庫,然後定義了兩個不同大小的元組。
然後,如上所述,numpy 庫需要陣列格式的資料才能處理它,因此元組被傳遞給 np.array,它將資料轉換為陣列格式。
一旦我們以陣列形式獲得了元組,np.column_stack 用於追加陣列的元素,並壓縮元組。
然後,使用 tuple() 函式將最終陣列再次轉換為元組。
輸出
以下程式碼的輸出將是
The output zipped tuple from tuple 1 and 2 is : ((7, 1), (8, 5), (4, 6), (5, 1))
結論
在本文中,我們討論了兩個不均勻元組或兩個不同大小(長度)元組的壓縮操作。上面討論的壓縮不均勻元組的三種不同方法將幫助人們瞭解壓縮操作,並在需要時幫助人們執行相同的操作。