Create list of factorials using list comprehension

前端 未结 3 1673
挽巷
挽巷 2021-01-23 11:26

I\'m trying to build a list of the first ten factorials

[1,1,2,6,24,120,720,5040,40320,362880]

using only list comprehension. Is that possible

3条回答
  •  耶瑟儿~
    2021-01-23 12:00

    You can use math.factorial():

    import math
    
    [math.factorial(n) for n in range(10)]
    

    Output:

    >>> import math
    >>> 
    >>> [math.factorial(n) for n in range(10)]
    [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
    

提交回复
热议问题