Python 库函数学习记录

当前正在写 leetcode 的题目,所以可能会经常用到 Python 的库函数,因此,在这里作一个记录,好记性不如烂键盘。

sort 函数

A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list:

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

You can also use the list.sort() method. It modifies the list in-place (and returns None to avoid confusion). Usually it’s less convenient than sorted() - but if you don’t need the original list, it’s slightly more efficient.

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

Another difference is that the list.sort() method is only defined for lists. In contrast, the sorted() function accepts any iterable.

>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
[1, 2, 3, 4, 5]

abs 函数

abs(x)

Return the absolute value of a number. The argument may be an integer, a floating point number, or an object implementing abs(). If the argument is a complex number, its magnitude is returned.


Python 库函数学习记录
http://fanyfull.github.io/2021/11/28/Python-库函数学习记录/
作者
Fany Full
发布于
2021年11月28日
许可协议