返回Python中复数参数的实部
要返回复数参数的实部,在Python中使用numpy.real()方法。该方法返回复数参数的实部。如果val是实数,则val的类型用于输出。如果val具有复数元素,则返回的类型为float。第一个参数val是输入数组。
步骤
首先,导入所需的库 –
import numpy as np
使用array()方法创建一个数组 −
arr = np.array([56.+0.j , 27.+0.j , 68.+0.j , 23.+0.j])
展示数组 –
print("Our Array...\n",arr)
检查尺寸−
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 –
print("\nDatatype of our Array object...\n",arr.dtype)
获取形状-
print("\nShape of our Array...\n",arr.shape)
要返回复数参数的实部,可以在Python中使用numpy.real()方法。该方法返回复数参数的实部。如果val是实数,则输出的类型为val的类型。如果val有复数元素,则返回的类型为float。第一个参数val是输入数组−
print("\nResult (Real part)...\n",np.real(arr))
示例
import numpy as np
# Create an array using the array() method
arr = np.array([56.+0.j , 27.+0.j , 68.+0.j , 23.+0.j])
# Display the array
print("Our Array...\n",arr)
# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)
# Get the Shape
print("\nShape of our Array...\n",arr.shape)
# To return the real part of the complex argument, use the numpy.real() method in Python
print("\nResult (Real part)...\n",np.real(arr))
输出
Our Array...
[56.+0.j 27.+0.j 68.+0.j 23.+0.j]
Dimensions of our Array...
1
Datatype of our Array object...
complex128
Shape of our Array...
(4,)
Result (Real part)...
[56. 27. 68. 23.]