使用Python中的map()函数进行格式化



我正在编写将mach转换为英尺/秒的代码,我想使用带有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函数。事实上,使用它是导致这个问题的原因。

machList上迭代时,x等于machList列表中的单个项。

所以x是一个整数1、3或5。

所以你所要做的就是:

def machToFPS(machList):
  for x in machList
    FPS = x * 1116.4370079
    print('{0}'.format(x), "mach(s) is equivalent to",FPS, "feet per second.")

这应该会输出您想要的文本。

为了使用映射功能获得相同的输出,您可以这样做:

def machToFPS(machList):
  fpsList = map(lambda x: x*1116.4370079,machList)
  for x in fpsList
    print('{0}'.format(x), "mach(s) is equivalent to",FPS, "feet per second.")

map(function, arg_list)返回一个列表,其中包含对数组arg_list的每个元素调用function的结果。您将需要迭代映射调用的结果。

一种方法是让lambda函数返回一个元组:

def mach_to_fps(mach_list):
    # Returning a tuple from the lambda allows the print to get all of its information
    # from the contents of the return list alone.
    for mach_fps_tuple in map(lambda mach: (mach, mach * 1116.4370079), mach_list):
        # *mach_fps_tuple breaks the tuple out into separate arguments for format.
        # Using old-style format would look like this:
        #    'mach(%s) is equivalent to %s feet per second' % mach_fps_tuple
        print('mach({0}) is equivalent to {1} feet per second'.format(*mach_fps_tuple))
mach_to_fps([1, 5, 3])
$ python mach.py
mach(1) is equivalent to 1116.4370079 feet per second
mach(5) is equivalent to 5582.1850395 feet per second
mach(3) is equivalent to 3349.3110237 feet per second

请注意,我使用PEP8进行了格式化,这是强烈推荐的。

你可以做:

>>> machList = [1,5,3]
>>> fmt='{} is equivalent to {} feet per second'
>>> map(lambda x: fmt.format(x, x*1116.4370079), machList)
['1 is equivalent to 1116.4370079 feet per second', '5 is equivalent to 5582.1850395 feet per second', '3 is equivalent to 3349.3110237 feet per second']

然后打印:

>>> print 'n'.join(map(lambda x: fmt.format(x, x*1116.4370079), machList))
1 is equivalent to 1116.4370079 feet per second
5 is equivalent to 5582.1850395 feet per second
3 is equivalent to 3349.3110237 feet per second

但我认为列表理解在这里更容易理解(可能更快),因为不需要lambda:

>>> print 'n'.join([fmt.format(x, x*1116.4370079) for x in machList])
1 is equivalent to 1116.4370079 feet per second
5 is equivalent to 5582.1850395 feet per second
3 is equivalent to 3349.3110237 feet per second

如果need使用map函数,则:

machList = [1,5,3]
def machToFPS(machList):
    FPS = map(lambda x: x*1116.4370079,machList) 
    for i,x in enumerate(FPS):
        print('{} mach(s) is equivalent to {} feet per second.'.format(machList[i], x))
machToFPS(machList)

如果没有,就按照下一个答案中显示的那样做(只使用for循环)。

输出:

1 mach(s) is equivalent to 1116.4370079 feet per second.
5 mach(s) is equivalent to 5582.1850395 feet per second.
3 mach(s) is equivalent to 3349.3110237 feet per second.

最新更新