Sort a part of a list in place

后端 未结 3 480
野性不改
野性不改 2020-11-29 03:39

Let\'s say we have a list:

a = [4, 8, 1, 7, 3, 0, 5, 2, 6, 9]

Now, a.sort() will sort the list in place. What if we want to sort only a

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 04:09

    if a is a numpy array then to sort [i, j) range in-place, type:

    a[i:j].sort()
    

    Example:

    >>> import numpy as np
    >>> a = np.array([4, 8, 1, 7, 3, 0, 5, 2, 6, 9])
    >>> a[1:4].sort()
    >>> a
    array([4, 1, 7, 8, 3, 0, 5, 2, 6, 9])
    

提交回复
热议问题