add two lists then sort = None(?)

前端 未结 2 770
Happy的楠姐
Happy的楠姐 2020-12-07 05:14

Second list squared each item on list, xs. Running the code below, python gives me \'None\'

xs = [12, 10, 32, 3, 66, 17, 42, 99, 20]
a = [b**2 for b in xs]
c         


        
2条回答
  •  时光取名叫无心
    2020-12-07 05:55

    You use generator expressions and itertools to reduce the amount of temporary storage like this

    >>> import itertools
    >>> xs = [12, 10, 32, 3, 66, 17, 42, 99, 20]
    >>> a = (b**2 for b in xs)
    >>> c = sorted(itertools.chain(a, xs))
    >>> c
    [3, 9, 10, 12, 17, 20, 32, 42, 66, 99, 100, 144, 289, 400, 1024, 1764, 4356, 9801]
    

提交回复
热议问题