首頁  >  文章  >  後端開發  >  如何在Python中壓縮不均勻的元組

如何在Python中壓縮不均勻的元組

WBOY
WBOY轉載
2023-08-30 11:09:121358瀏覽

如何在Python中壓縮不均勻的元組

簡介

在Python中,元組是廣泛使用的根據需求儲存和處理資料的方法之一。元組中涉及很多操作,其中資料根據問題陳述的要求進行預處理和轉換。壓縮操作是壓縮不同元組的最常見和最廣泛使用的操作之一。

在本文中,我們將討論 Python 中不均勻元組的壓縮、不均勻元組壓縮的實際意義,以及透過程式碼解釋執行相同操作的不同方法。本文將幫助人們了解壓縮不均勻元組背後的核心思想,並幫助人們在必要時做同樣的事情。

現在讓我們先討論Python中壓縮和Python中不均勻元組壓縮的意義。

什麼是非均勻元組壓縮?

在Python中,zip或zipping這個字表示我們正在將不同元組的元素相加,這意味著我們正在製作一對不同元組的元素並將其儲存在單一公共元組中。

例如,如果我們有兩個像這樣的元組:

T1 = (1, 2, 3)

T2 = (“一”, “二”, “三”)

然後對這些元組進行壓縮操作將給出以下輸出:

T_Zip = ((, “一個”), (2, “兩個”), (3, “三個”))

這裡的不均勻元組是指兩個元組的大小或長度不相同,即其中一個元組的大小比另一個元組小或大。對於具有相同大小或長度的元組來說,壓縮操作是一項非常簡單的任務,但是當壓縮兩個不同大小或不均勻的元組時,壓縮操作就變得非常複雜。

但是,有一些方法可以用來壓縮兩個不均勻的元組。讓我們一一討論一下。

壓縮不均勻元組

在 Python 中,我們主要使用三種方式來壓縮不均勻元組。

  • 使用 For 迴圈和枚舉

  • 使用列表理解

  • #使用 Numpy 函式庫

方法一:使用For迴圈和列舉

我們可以使用 for 迴圈和枚舉函數來壓縮不均勻元組。它是執行此操作的最簡單且有效的客戶端方法之一。

# 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 迴圈與枚舉一起使用,枚舉附加元組 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 循環附加兩個元組並返回輸出。

Output

以下程式碼的輸出為:

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)]

方法三:使用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() 函數再次將最終數組轉換為元組。

Output

以下程式碼的輸出為:

The output zipped tuple from tuple 1 and 2 is : ((7, 1), (8, 5), (4, 6), (5, 1))

結論

在本文中,我們討論了兩個不均勻元組或兩個不同大小(長度)元組的壓縮操作。上面討論的壓縮不均勻元組的三種不同方法將幫助人們理解壓縮操作,並幫助人們在必要時執行相同的操作。

以上是如何在Python中壓縮不均勻的元組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除