python list comprehension to produce two values in one iteration

后端 未结 12 1963
刺人心
刺人心 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:57

    As mentioned, itertools is the way to go. Here's how I would do it, I find it more clear:

    [i if turn else i*i for i,turn in itertools.product(range(1,10), [True, False])]
    

提交回复
热议问题