Variable assignment in expressions

后端 未结 4 1927
傲寒
傲寒 2021-01-16 13:36

Here is my code to generate values in the fibonnacci sequence below 10,000,000.

  3 fibs = [1,1]
  4 while((x = fibs[-1] + fibs[-2]) <= 10000000):
  5             


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 14:29

    from functools import partial
    from itertools import imap, islice, takewhile
    import operator
    fibs = [1, 1]
    for x in takewhile(partial(operator.ge, 10000000),
                       imap(operator.add, fibs, islice(fibs, 1, None))):
        fibs.append(x)
    

    Oh wait, you said "simplest"? Nevermind then.

提交回复
热议问题