簡體   English   中英

在Python中使用map()函數進行格式化

[英]Formatting with the map() function in Python

我正在編寫代碼以將馬赫數轉換為英尺/秒,並且我想將map函數與lambda運算符一起使用來進行轉換。 我有一個名為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功能。 實際上,使用它是導致此問題的原因。

當您遍歷machListx等於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.")

那應該輸出您想要的文本。

為了使用map函數獲得相同的輸出,可以執行以下操作:

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)返回一個列表,其中包含調用function的結果以及數組arg_list每個元素。 您將需要迭代地圖調用的結果。

一種方法是讓您的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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM