比较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)
错误:
typeerror:未支撑的操作数类型 - :'list'and'list'
我试图比较python中的两个浮点列表,但出现了错误。如果匹配,则显示无与伦比。谁能帮助我,并为我提供另一种方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码中没有Numpy。
您有字符串列表 - 而不是浮子。您使用的方法在数字上而不是字符串上操作。
有(对于浮子)现有的方法可以比较浮子:
如果要比较列表,则需要迭代列表。如果要检查浮子,则需要在检查之前转换为float:
输出:
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:
Output:
您不能将“ - ”运算符与列表一起使用。代码
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 doa-b
which it cannot do asa
andb
are lists.You can write a function to subtract each index in list
b
from each corresponding index in lista
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'snumpy.absolute()
.使用Python,您无法互相提取列表对象。此外,您不能互相提取str对象。
您可以使用Oneline进行:
返回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 :
return True if both elements of lists are close
Greetings