如何將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日

9000+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.