如何将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.