如何將 Python 元組拆分為子元組?


在本文中,我們將向您展示如何將 Python 元組拆分為子元組。以下是完成此任務的各種方法:

  • 使用切片

  • 使用 enumerate() 和模運算子

元組是一種不可變的、無序的資料型別,用於在 Python 中儲存集合。列表和元組在許多方面相似,但列表的長度可變且是可變的,而元組的長度固定且不可變

使用切片

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入元組。

  • 列印輸入元組。

  • 要將輸入元組拆分為 n(3)個元素,使用range() 函式(返回一個從 0 開始並以 1(預設)遞增並在給定數字之前停止的數字序列),從 0 迴圈到元組的長度使用len() 函式(len() 方法返回物件中的專案數),以 n 作為步長值。

  • 對於每次迭代,將當前迭代器到當前迭代器索引 +n 值的所有資料儲存起來(子元組)

  • 將結果元組拆分為 4 個元組的組後打印出來。

示例

以下程式使用切片將給定的 Python 元組拆分為子元組:

# input tuple inputTuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) # printing input tuple print ("Input Tuple:", inputTuple) # Giving the Number of sub-tuples required n = 3 # splitting the input tuple with n(3) elements # Looping from 0 indexes to length of tuple with n as step value using the range() function # For each iteration storing all values from the current iterator to the current iterator index +n values(sub tuples) resultTuple = tuple(inputTuple[i:i + n] for i in range(0, len(inputTuple), n)) # printing the result tuple print ("Splitting the input tuple into the group of 4-tuples with 3 elements in each:\n", resultTuple)

輸出

執行上述程式後,將生成以下輸出:

Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
Splitting the input tuple into group of 4-tuples with 3 elements in each:
((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))

使用 enumerate() 和模運算子

enumerate() 方法為可迭代物件新增一個計數器,並返回 enumerate 物件。

語法

enumerate(iterable, start=0)

引數

  • iterable - 它可以是任何支援迭代的序列/物件/可迭代物件

  • start - enumerate() 從此值開始計數。如果未指定 start,則使用值 0。

模運算子(%)

在 Python 中,% 符號稱為模運算子。它給出左運算元除以右運算元的結果。它用於透過獲取餘數來解決除法問題。

示例

以下程式使用 enumerate() 和模運算子將給定的 Python 元組拆分為子元組:

# input tuple inputTuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) # printing input tuple print ("Input Tuple:", inputTuple) # splitting the input tuple with n(3) elements # Looping in the given tuple using the enumerate() function # Splitting into tuples of size 3 so we used n%3==0 condition resultTuple = tuple(inputTuple[e:e + 3] for e, k in enumerate(inputTuple) if e % 3 == 0) # printing the result tuple print ("Splitting the input tuple into the group of 4-tuples with 3 elements in each:\n", resultTuple)

輸出

執行上述程式後,將生成以下輸出:

Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
Splitting the input tuple into group of 4-tuples with 3 elements in each:
((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))

使用 itertools 食譜

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用def關鍵字建立一個函式splitTupleandGroup(),透過將 k、輸入元組作為引數傳遞給它,將輸入元組拆分為 k 個元組(此處為每個包含 3 個元素的 4 個元組)的組。

  • 將給定的輸入元組傳遞給iter() 函式(Python 中的 iter() 函式返回一個迭代器物件,用於將可迭代物件轉換為迭代器),並使用 [] 運算子將其轉換為列表,並將其乘以所需的子元組數(k)。

  • 使用zip() 函式(zip() 函式可用於組合兩個列表/迭代器)組合所有上述子列表,方法是使用 * 運算子解包它們並將其作為引數傳遞。

  • 建立一個變數來儲存輸入元組。

  • 列印輸入元組。

  • 呼叫splitTupleandGroup()函式進行分組以建立子元組。

  • 將結果元組拆分為 4 個元組的組後打印出來。

示例

以下程式使用 iter() 和 zip() 函式將給定的 Python 元組拆分為子元組:

# creating a function to split the input tuple into groups of # k tuples(here 4-tuples with 3 elements) def splitTupleandGroup(k, inputTuple): # Multiply the given tuple with the number of sub-tuples needed args = [iter(inputTuple)] * k # Return the zip object of the above args return zip(*args) # input tuple inputTuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) # printing input tuple print ("Input Tuple:", inputTuple) # calling the splitTupleandGroup function to group the k elements of the tuple # and converting into a tuple resultTuple = tuple(splitTupleandGroup(3, inputTuple)) # printing the result tuple print ("Splitting the input tuple into a group of 4-tuples with 3 elements in each:\n", resultTuple)

輸出

執行上述程式後,將生成以下輸出:

Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
Splitting the input tuple into group of 4 tuples with 3 elements in each:
((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))

結論

在這篇文章中,我們學習了三種將 Python 元組劃分為子元組的替代方法。我們還學習瞭如何將元組轉換為迭代器。如何將迭代器轉換為列表,解包列表並將它們作為元組返回

更新於:2022-11-09

9K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告