python list comprehension to produce two values in one iteration

后端 未结 12 1975
刺人心
刺人心 2021-02-03 17:20

I want to generate a list in python as follows -

[1, 1, 2, 4, 3, 9, 4, 16, 5, 25 .....]

You would have figured out, it is nothing but n,

12条回答
  •  情话喂你
    2021-02-03 17:44

    Lots of tricks in this thread. Here is another using a one liner generator without imports

    x = (lamdba : [[(yield i), (yield i**2)] for i in range(10)])()
    

    EDIT: This will raise DeprecatedWarning in Python 3.7 and SyntaxError in Python 3.8: https://docs.python.org/dev/whatsnew/3.7.html#deprecated-python-behavior

提交回复
热议问题