# python绝对值_如何在Python中获得数字的绝对值？

＃在Python中获取绝对值的两种方法

Python有两种获取数字绝对值的方法：

＃使用Pythonabs()函数获取绝对值

abs(-23.43)

# Returns: 23.43

＃获取Python中整数的绝对值

# Some random integers

variableA = 28

variableB = -101

variableC = 0

variableD = -9116439

# Get the absolute values of those integers

absA = abs(variableA)

absB = abs(variableB)

absC = abs(variableC)

absD = abs(variableD)

# Output the results

print("Absolute values of integers with abs():")

print("|", variableA, "| = ", absA, sep="")

print("|", variableB, "| = ", absB, sep="")

print("|", variableC, "| = ", absC, sep="")

print("|", variableD, "| = ", absD, sep="")

Absolute values of integers with abs():

|28| = 28

|-101| = 101

|0| = 0

|-9116439| = 9116439

＃获取Python中浮点值的绝对值

# Some random floating-point values

variableA = -12.34

variableB = -1.8457425364

variableC = -0.00000000001

variableD = 9424.5895279095

# Get the absolute value of each variable

absVarA = abs(variableA)

absVarB = abs(variableB)

absVarC = abs(variableC)

absVarD = abs(variableD)

# Output the results

print("Absolute values of floating point values with abs():")

print("|", variableA, "| = ", absVarA, sep="")

print("|", variableB, "| = ", absVarB, sep="")

print("|", variableC, "| = ", absVarC, sep="")

print("|", variableD, "| = ", absVarD, sep="")

|-12.34| = 12.34

|-1.8457425364| = 1.8457425364

|-1e-11| = 1e-11

|9424.5895279095| = 9424.5895279095

＃使用Pythonmath.fabs()函数获取绝对浮点值

import math

math.fabs(-8)

# Returns: 8.0

＃示例：获取绝对值作为浮点值

import math

# Some random values

valueA = -4

valueB = -56

valueC = 26

valueD = -2.992474203

# Get the floating-point absolute value from each

fabs_A = math.fabs(valueA)

fabs_B = math.fabs(valueB)

fabs_C = math.fabs(valueC)

fabs_D = math.fabs(valueD)

# Output the results

print("Absolute floating-point values with fabs():")

print("|", valueA, "| = ", fabs_A, sep="")

print("|", valueB, "| = ", fabs_B, sep="")

print("|", valueC, "| = ", fabs_C, sep="")

print("|", valueD, "| = ", fabs_D, sep="")

Absolute floating-point values with fabs():

|-4| = 4.0

|-56| = 56.0

|26| = 26.0

|-2.992474203| = 2.992474203

＃从Python列表或数组中获取绝对值

＃获取Python列表中数字的绝对值

Python有几种获取每个列表值的绝对值的方法。一种选择是列表理解表达式。这就提供了一种干净有效的方法，可以将完整列表转换为绝对值。

# Some random numbers

values = [-40, 58, -69, -84, 51, 76, -12, 36]

# Get the absolute value for each number

absValues = [abs(number) for number in values]

# Output data

print("Original numbers:\n", values)

print("Absolute values:\n", absValues)

Original numbers:

[-40, 58, -69, -84, 51, 76, -12, 36]

Absolute values:

[40, 58, 69, 84, 51, 76, 12, 36]

# Some example values

values = [-40, 58, -69, -84, 51, 76, -12, 36]

# Replace the original numbers with the absolute values

values = [abs(number) for number in values]

＃使用Python循环从列表中获取绝对值

# Some positive and negative values

values = [-40, 58, -69, -84, 51, 76, -12, 36]

# Take the absolute value of negative values, but

# multiply positive values with 2

processed = []

for number in values:

if number < 0:

processed.append(abs(number))

else:

processed.append(number * 2)

# Output data

print("Original numbers:\n", values)

print("Processed numbers:\n", processed)

Original numbers:

[-40, 58, -69, -84, 51, 76, -12, 36]

Processed numbers:

[40, 116, 69, 84, 102, 152, 12, 72]

# Some positive and negative values

values = [-40, 58, -69, -84, 51, 76, -12, 36]

# Loop through the list and replace

# each number with its absolute value

for index, value in enumerate(values):

values[index] = abs(value)

＃从Python数组获取绝对值

import array

# Create an array with random values

values = array.array('i', [-40, 58, -69, -84, 51, 76, -12, 36])

# Make a new array with the absolute values

absValues = array.array('i', [abs(value) for value in values])

# Output the results

print("Original array values:\n", values)

print("Absolute values:\n", absValues)

Original array values:

array('i', [-40, 58, -69, -84, 51, 76, -12, 36])

Absolute values:

array('i', [40, 58, 69, 84, 51, 76, 12, 36])

values = array.array('i', [-40, 58, -69, -84, 51, 76, -12, 36])

# Overwrite existing array with absolute values

values = array.array('i', [abs(value) for value in values])

＃从NumPy数组中获取绝对值

import numpy

values = numpy.array([-40, 58, -69, -84, 51, 76, -12, 36])

# Make a new, absolute value array

absValues = numpy.abs(values)

print("Original NumPy array values:\n", values)

print("Absolute values:\n", absValues)

Original NumPy array values:

[-40 58 -69 -84 51 76 -12 36]

Absolute values:

[40 58 69 84 51 76 12 36]

values = numpy.array([-40, 58, -69, -84, 51, 76, -12, 36])

# Overwrite array to get absolute values only

values = numpy.abs(values)

Python的abs()函数返回整数和浮点数的绝对值。要使用它，我们调用函数并为其提供我们想要绝对值的参数。

math.fabs()Python中还有一个函数。该值还返回绝对值，但始终为浮点值。

• 2
点赞
• 4
收藏
• 0
评论
10-12 4020
02-12 410
11-20 6080
12-13 1万+
08-06 1万+
07-22 1711
09-16 514
11-15 2万+
08-19 3万+
04-27 3758
04-02 8829
09-16 1094
12-12 607
11-05 1万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。