比较python中的两个浮点列表字符串

发布于 2025-01-30 05:46:41 字数 370 浏览 3 评论 0 原文

a=['0.0','123.34'] #list
b=['0.0','123.34']

def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
    return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

isclose(a,b)

错误:

  typeerror:未支撑的操作数类型 - :'list'and'list'
 

我试图比较python中的两个浮点列表,但出现了错误。如果匹配,则显示无与伦比。谁能帮助我,并为我提供另一种方法。

a=['0.0','123.34'] #list
b=['0.0','123.34']

def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
    return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

isclose(a,b)

Error:

TypeError: unsupported operand type(s) for -: 'list' and 'list'

I tried to compare the two float lists in python but got the error. if matched otherwise show unmatched. Can anyone help me and provide me with an alternative way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

得不到的就毁灭 2025-02-06 05:46:41

您的代码中没有Numpy。

您有字符串列表 - 而不是浮子。您使用的方法在数字上而不是字符串上操作。

有(对于浮子)现有的方法可以比较浮子:

如果要比较列表,则需要迭代列表。如果要检查浮子,则需要在检查之前转换为float:

# use what already exists
from math import isclose

a = ['0.0','123.34','42'] 
b = ['0.0','123.34','3.141']

r = []

# same length lists, use zip to iterate pairwise, use enumerate for index
for idx, (aa, bb) in enumerate(zip(a,b)):
    # convert to floats
    aaa = float(aa)
    bbb = float(bb)

    # append if not close
    if not isclose(aaa,bbb):
        r.append((idx, (aaa,bbb)))

# print results
for w in r:
    print("On index {0} we have {1} != {2}".format(w[0],*w[1]), sep="\n")

输出:

On index 2 we have 42.0 != 3.141

There is no numpy in your code.

You have lists of strings - not floats. The method you use operates on numbers, not strings.

There is (for floats) an existing method to compare floats: math.isclose

If you want to compare lists you need to iterate the list. If you want to check floats, you need to convert to float before checking:

# use what already exists
from math import isclose

a = ['0.0','123.34','42'] 
b = ['0.0','123.34','3.141']

r = []

# same length lists, use zip to iterate pairwise, use enumerate for index
for idx, (aa, bb) in enumerate(zip(a,b)):
    # convert to floats
    aaa = float(aa)
    bbb = float(bb)

    # append if not close
    if not isclose(aaa,bbb):
        r.append((idx, (aaa,bbb)))

# print results
for w in r:
    print("On index {0} we have {1} != {2}".format(w[0],*w[1]), sep="\n")

Output:

On index 2 we have 42.0 != 3.141
小红帽 2025-02-06 05:46:41

您不能将“ - ”运算符与列表一起使用。代码 abs(ab)首先尝试执行 ab ,它不能作为 a b 是列表。

您可以从列表中的每个相应索引 a 中的每个相应索引中写下函数,以减去列表 b 中的每个索引,并将每个计算的结果放在具有相同索引的新列表中。

或者,使用numpy的 numpy.subtract()方法来执行此计算。

eforeemore, abs()无法在列表对象上使用,您可以编写自己的代码,该代码在列表中的每个元素上迭代并计算每个元素的绝对值或使用numpy的 numpy。绝对()

You cannot use the '-' operator with lists. The code abs(a-b) is first trying to do a-b which it cannot do as a and b are lists.

You can write a function to subtract each index in list b from each corresponding index in list a and place the result of each computation in a new list with the same index.

Alternatively use numpy's numpy.subtract() method to carry out this computation.

Furtheremore, abs() cannot be used on a list object, you can write your own code that iterates over each element in a list and calculates the absolute value of each element or use numpy's numpy.absolute().

£噩梦荏苒 2025-02-06 05:46:41

使用Python,您无法互相提取列表对象。此外,您不能互相提取str对象。

您可以使用Oneline进行:

a=['0.0','123.34'] #list
b=['0.0','123.34']

def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):

    return all([True if abs(float(i)-float(j)) <= max(rel_tol * max(abs(float(i)), abs(float(j))), abs_tol) else False for i,j in zip(a,b) ])


isclose(a,b)

返回true如果列表的两个要素都是密切的

问候

With python you can't substract list object from each other. Moreover, you can't substract str object from each other.

You can do it with oneline :

a=['0.0','123.34'] #list
b=['0.0','123.34']

def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):

    return all([True if abs(float(i)-float(j)) <= max(rel_tol * max(abs(float(i)), abs(float(j))), abs_tol) else False for i,j in zip(a,b) ])


isclose(a,b)

return True if both elements of lists are close

Greetings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文