如何將 Python 元組轉換為 C 陣列?


在本文中,我們將向您展示如何在 Python 中將 Python 元組轉換為 C 陣列。

Python 沒有像其他程式語言那樣的內建陣列資料型別,但您可以使用像Numpy這樣的庫來建立陣列。

以下是將元組轉換為 Python 中陣列的各種方法:

  • 使用 numpy.asarray() 方法

  • 使用 numpy.array() 方法

如果您尚未在系統上安裝NumPy,請執行以下命令進行安裝。

pip install numpy

使用 numpy.asarray() 將元組轉換為陣列

使用np.asarray()函式將 Python 元組轉換為陣列。庫函式np.asarray()將輸入轉換為陣列。列表、元組、元組、元組的元組、列表的元組和ndarrays都是輸入。

演算法(步驟)

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

  • 使用 import 關鍵字匯入 NumPy 模組並使用別名。

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

  • 列印輸入元組。

  • 使用type()函式(返回物件的數

  • 使用numpy.asarray()函式透過將輸入元組作為引數傳遞給它來將輸入元組轉換為陣列。

  • 在將輸入 Python 元組轉換為陣列後列印輸出陣列。

  • 使用type()函式(返回物件的

示例

以下程式使用numpy.asarray()函式將輸入元組轉換為陣列:

# importing NumPy module with an alias name import numpy as np # input tuple inputTuple = (12, 1, 3, 18, 5) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tuple print("Type of InputTuple:", type(inputTuple)) # converting python tuple to an array using numpy.asarray() function outputArray = np.asarray(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple to array:\n", outputArray) # printing the data type of the output array print("Type of OutputArray:", type(outputArray))

輸出

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

InputTuple: (12, 1, 3, 18, 5)
Type of InputTuple: 
Output array after conversion of input tuple to array:
[12 1 3 18 5]
Type of OutputArray: <class 'numpy.ndarray'="">

使用 numpy.asarray() 將列表的元組轉換為陣列

演算法(步驟)

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

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

  • 列印輸入元組。

  • 使用type()函式(返回物件的數

  • 使用numpy.asarray()函式透過將輸入元組作為引數傳遞給它來將輸入列表的元組轉換為陣列。

  • 在將輸入列表的元組轉換為陣列後列印輸出陣列。

  • 使用type()函式(返回物件的

  • 在輸出陣列上應用flatten()函式(將 ndarray 扁平化為一維)以將輸出陣列扁平化為一維。

  • 列印結果扁平化陣列。

示例

以下程式使用numpy.asarray()函式將輸入列表的元組轉換為陣列,並返回其扁平化陣列:

# importing NumPy module with an alias name import numpy as np # input tuple inputTuple = ([1, 10, 5], [3, 6, 4]) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tupl print("Type of Input Tuple:", type(inputTuple)) # converting python tuple of lists to an array using numpy.asarray() function outputArray = np.asarray(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple of lists to array:\n", outputArray) # printing the data type of the output array print("Type of Output Array:", type(outputArray)) # flattening the output array to 1-dimension flatten_array = outputArray.flatten() # printing the flattened array print("Flattened Array:", flatten_array)

輸出

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

InputTuple: ([1, 10, 5], [3, 6, 4])
Type of Input Tuple: <class 'tuple'>
Output array after conversion of input tuple of lists to array:
[[ 1 10 5]
[ 3 6 4]]
Type of Output Array: <class 'numpy.ndarray'>
Flattened Array: [ 1 10 5 3 6 4]

numpy.asarray()函式根據列表的元組建立陣列。但是,它將建立一個二維陣列,可以使用array.flatten()方法將其展平。

使用 numpy.array() 將元組轉換為陣列

numpy.array()函式接受 Python 物件並返回陣列。我們將傳遞一個元組物件到np.array()函式,它將將其轉換為陣列。

示例

以下程式使用numpy.array()函式將輸入元組轉換為陣列:

# importing numpy module with an alias name import numpy as np # input tuple inputTuple = (12, 1, 3, 18, 5) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tuple print("Type of InputTuple:", type(inputTuple)) # converting python tuple to an array using numpy.array() function outputArray = np.array(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple to array:\n", outputArray) # printing the data type of the output array print("Type of OutputArray:", type(outputArray))

輸出

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

InputTuple: (12, 1, 3, 18, 5)
Type of InputTuple: <class 'tuple'>
Output array after conversion of input tuple to array:
[12 1 3 18 5]
Type of OutputArray: <class 'numpy.ndarray'>

使用 numpy.array() 將列表的元組轉換為陣列

示例

以下程式使用numpy.array()函式將輸入列表的元組轉換為陣列,並返回其扁平化陣列:

# importing numpy module with an alias name import numpy as np # input tuple inputTuple = ([1, 10, 5], [3, 6, 4]) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tuple print("Type of Input Tuple:", type(inputTuple)) # converting python tuple of lists to an array using numpy.array() function outputArray = np.array(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple of lists to array:\n", outputArray) # printing the data type of the output array print("Type of Output Array:", type(outputArray)) # flattening the output array to 1-dimension flatten_array = outputArray.flatten() # printing the flattened array print("Flattened Array:", flatten_array)

輸出

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

InputTuple: ([1, 10, 5], [3, 6, 4])
Type of Input Tuple: <class 'tuple'>
Output array after conversion of input tuple of lists to array:
[[ 1 10 5]
[ 3 6 4]]
Type of Output Array: <class 'numpy.ndarray'>
Flattened Array: [ 1 10 5 3 6 4]

結論

在這篇文章中,我們學習瞭如何使用 Numpy 模組的array()asarray()函式將 Python 元組轉換為 C 陣列。在這篇文章中,我們還學習瞭如何將元組列表轉換為陣列並將其展平。

更新於: 2022年11月9日

9K+ 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.