我正在写代码将马赫数转换成英尺/秒,我想用lambda操作符的map函数来转换它。我有一个参数,叫做machList。我可以成功地转换,但我有麻烦格式化它正确,所以答案不出来作为整个列表转换的答案。你知道吗
到目前为止,我的代码如下所示:
def machToFPS(machList):
for x in machList:
FPS = map(lambda x: x*1116.4370079,machList)
print('{0}'.format(x), "mach(s) is equivalent to",FPS, "feet per second.")
如果machList=[1,5,3],我得到的输出是:
1 mach(s) is equivalent to [1116.4370079, 5582.1850395, 3349.3110237] feet per second.
5 mach(s) is equivalent to [1116.4370079, 5582.1850395, 3349.3110237] feet per second.
3 mach(s) is equivalent to [1116.4370079, 5582.1850395, 3349.3110237] feet per second.
I want my answer to look like this:
1 mach is equivalent to 1116.4370079 feet per second.
5 mach is equivalent to 5582.1850395 feet per second.
3 mach is equivalent to 3349.3110237 feet per second.
有人能帮我吗?map()函数把我搞砸了。你知道吗
map(function, arg_list)
返回一个列表,其中包含使用数组arg_list
的每个元素调用function
的结果。您需要迭代map调用的结果。你知道吗一种方法是让lambda函数返回一个元组:
注意,我使用PEP8格式化了它,这是强烈推荐的。你知道吗
如果
need
要使用map
函数,则:如果没有,就按照下一个答案中显示的那样做(只使用for循环)。你知道吗
输出:
您不需要使用
map
函数。事实上,使用它是导致这个问题的原因。你知道吗在
machList
上迭代时,x
等于machList
列表中的单个项。你知道吗所以
x
是一个整数1、3或5。你知道吗所以你要做的就是:
这样就可以输出你想要的文本了。你知道吗
为了使用map函数获得相同的输出,可以执行以下操作:
相关问题 更多 >
编程相关推荐