美文网首页
【Python】sort

【Python】sort

作者: 盐果儿 | 来源:发表于2024-09-06 00:40 被阅读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]

相关文章

网友评论

      本文标题:【Python】sort

      本文链接:https://www.haomeiwen.com/subject/ebudljtx.html