【Python】sort

2024-09-06  本文已影响0人  盐果儿

普通数组

1. sort()

my_list = [3, 1, 4, 1, 5]

my_list.sort()

# 原地排序print(my_list)

# 输出: [1, 1, 3, 4, 5]

2. sorted()

my_list = [3, 1, 4, 1, 5]

sorted_list = sorted(my_list)

# 原地排序

print(sorted_list)

# 输出: [1, 1, 3, 4, 5]

Numpy 数组

import numpy as np

arr = np.array([3, 1, 4, 1, 5])

sorted_arr = np.sort(arr)

print(sorted_arr)

# 输出: [1, 1, 3, 4, 5]

上一篇 下一篇

猜你喜欢

热点阅读