gpt4 book ai didi

python 数学模块,列表中的 log10

转载 作者:行者123 更新时间:2023-12-01 03:54:24 25 4
gpt4 key购买 nike

我试图在我的列表中使用 log10 函数,但是每次我都会遇到“需要 float 错误”,即使我将列表转换为 float 。我不明白我做错了什么。这是我到目前为止的代码:

#!/usr/bin/python
import math
import operator

fileq= open("test.ppx1","r")
filer= open("test.prob1","r")
words = list(fileq.read().split())
words2 = list(filer.read().split())
words[:]=[x[:8] for x in words]
words2[:]=[x[:8] for x in words2]
id1= words[-1]
id2=words2[-1]
words.remove(id1)
words2.remove(id2)
map(float,words)
map(float,words2)


[math.log(y,10) for y in words]
[math.log(y,10) for y in words2]

这是我不断收到的错误:

TypeError: a float is required

最佳答案

map(float, words)
map(float, words2)

map 未就位。它返回一个新列表(在 Python 2 中。在 Python 3 中它返回一个 map 对象)。以及对给定可迭代项执行函数的结果。

将这些行更改为:

words = map(float, words)
words2 = map(float, words2)

关于python 数学模块,列表中的 log10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37743921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com