add two lists then sort = None(?)

前端 未结 2 777
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:48

    Generally speaking, anything that operates on something in-place will return None, by convention. (This convention is not necessarily always followed, however.) somelist.sort() will sort the list in-place.

    If you'd rather have a sorted copy, you can just call c = sorted(a + xs). sorted operates on a copy of the original, and therefore returns the copy.

    There's a much more through explanation here: http://wiki.python.org/moin/HowTo/Sorting/

提交回复
热议问题