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


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

  • 使用切片

  • 使用enumerate()和取模運算子

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

使用切片

演算法(步驟)

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

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

  • 列印輸入元組。

  • 要將輸入元組拆分成n(3)個元素,使用range()函式(返回一個從0開始,每次遞增1(預設),並在到達給定數字之前停止的數字序列),使用len()函式(len()方法返回物件中的專案數)從0迴圈到元組的長度,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 recipes

演算法(步驟)

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

  • 使用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月9日

9K+ 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.