Python - 巢狀列表轉換為單值元組


在給定的問題陳述中,我們必須藉助 Python 功能將給定的巢狀列表轉換為單值元組。因此,我們將使用不同的技術在 python 中解決此任務。

理解問題

眼前的問題是使用巢狀列表建立一個單值元組並在 Python 中實現程式碼。有時我們需要在競賽程式設計或公司面試中解決此類問題。巢狀列表允許我們在單個列表中儲存多個子列表。單值元組意味著在一個元組中應該只有一個值。所以基本上我們必須建立一個演算法將巢狀列表轉換為單個元組值。

上述問題的邏輯

給定的問題可以使用不同的方法解決。但在我們的文章中,我們將透過三種方式解決此問題。第一種方法將使用 reduce 函式。第二種方法將使用迴圈。在第三種方法中,我們將使用 Python 的 numpy 庫。

演算法 - 使用 Reduce 函式

  • 步驟 1 - 首先,我們將從 Python 的 functools 庫匯入 reduce 函式。

  • 步驟 2 - 之後,我們將定義函式為 convert_the_list 並傳遞一個引數作為 the_list,它是巢狀列表。

  • 步驟 3 - 現在,我們將使用 functools 中的 reduce 函式來展平給定的巢狀列表。

  • 步驟 4 - 然後,我們將建立一個新列表,為給定列表中的每個值建立一個元組。

  • 步驟 5 - 最後,我們將返回列表中單個元組值的輸出。

示例 - 使用 Reduce 函式

# Import the reduce function
from functools import reduce
#Function to convert nested list in single tuple value
def convert_the_list(the_list):
   single_list = reduce(lambda a, b: a+b, the_list)
   result = [(a,) for a in single_list]
   return result

#define the nested list
nested_list = [[12, 20], [15, 17, 16], [36], [45, 87]]

# Show the input nested list
print("Input Nested list : " + str(nested_list))

Output = convert_the_list(nested_list)

# Print the result
print("Single tuple value list: " + str(Output))

輸出

Input Nested list : [[12, 20], [15, 17, 16], [36], [45, 87]]
Single tuple value list: [(12,), (20,), (15,), (17,), (16,), (36,), (45,), (87,)]

複雜度

使用 reduce 函式將巢狀列表轉換為單值元組的程式碼為 O(n²) ,其中 n 是給定巢狀列表中專案的總數。因為我們使用了具有 O(n²) 時間複雜度的 reduce 函式,這是造成這種時間複雜度的原因。

演算法 - 使用 for 迴圈

  • 步驟 1 - 定義名為 convert_the_list 的函式,並傳遞一個引數 the_list,該引數表示輸入巢狀列表。

  • 步驟 2 - 初始化一個名為 the_tuple 的空物件來儲存單值元組。

  • 步驟 3 - 現在使用 for 迴圈遍歷巢狀列表的專案,並使用巢狀迴圈遍歷巢狀列表專案。

  • 步驟 4 - 在迭代過程中,我們將每個專案追加到 the_tuple 物件中。

  • 步驟 5 - 並返回 the_tuple 的值以獲取單值元組列表。

示例 - 使用 for 迴圈

#function to convert nested list in single value tuple
def convert_the_list(the_list):
   the_tuple = []
   for sub_list in the_list:
      for val in sub_list:
         the_tuple.append((val,))
   return the_tuple
#Initialize the nested lists
nested_list = [[1, 5, 6], [4, 8, 9]]
Output = convert_the_list(nested_list)
print("The Single value tuple:", Output)

nested_list1 = [[12, 14, 16, 18]]
Output1 = convert_the_list(nested_list1)
print("The Single value tuple:", Output1)

輸出

The Single value tuple: [(1,), (5,), (6,), (4,), (8,), (9,)]
The Single value tuple: [(12,), (14,), (16,), (18,)]

複雜度

使用 for 迴圈將巢狀列表轉換為單值元組列表的時間複雜度為 O(m * n) ,其中 m 是輸入列表中子列表的數量,n 是列表中專案的總數。

演算法 - 使用 Numpy

  • 步驟 1 - 首先,使用 import 關鍵字將 numpy 庫匯入為 nmp。

  • 步驟 2 - 定義函式為 convert_the_list 並傳遞一個引數作為 the_list,它是巢狀輸入列表。

  • 步驟 3 - 現在,我們首先使用 numpy 在函式內部將給定列表轉換為陣列。

  • 步驟 4 - 然後,我們將展平給定的陣列並使用 flatten 方法刪除子陣列。

  • 步驟 5 - 由於我們現在有了展平的陣列,我們將使用 tuple 函式將其轉換為元組。並返回 Output_tuple 以在 Output 中顯示。

示例 - 使用 Numpy

# Import the numpy library
import numpy as nmp

def convert_the_list(the_list):
   # convert the list into an array
   the_array = nmp.array(the_list)
   # Flatten the above array
   flat_array = the_array.flatten()
   # convert the flattened array into the tuple format
   Output_tuple = tuple(flat_array)
   return Output_tuple

# Initialize the input nested list
the_nested_list = [[14, 17, 13], [11, 18, 15], [12, 19, 16]]
the_result = convert_the_list(the_nested_list)
print("The converted single tuple: ", the_result)

輸出

The converted single tuple:  (14, 17, 13, 11, 18, 15, 12, 19, 16)

複雜度

使用 numpy 庫將巢狀列表轉換為單個元組值的時間複雜度為 O(n) ,因為我們首先將列表轉換為大小為 n 的陣列,然後再次將陣列轉換為元組。

結論

因此,我們已經成功地解決了將巢狀列表更改為使用 Python 的單值元組的給定問題。正如我們已經使用了三種方法來解決給定的問題。所有程式碼都具有不同的時間複雜度。並且我們學習了 reduce 函式、for 迴圈和 numpy 庫函式的使用。

更新於: 2023年10月17日

93 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.