sorted(iterable, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
从包含新项的iterable列表中升序返回。
A custom key function can be supplied to customise the sort order,and the
reverse flag can be set to request the result in descending order.
可以提供自定义键函数来自定义排序顺序,并且reverse可以设置为True按降序排序
List.sort(key=None, reverse=False)
method of builtins.list instance
列表内置方法
print(a)'alsouefgb'print(a.sort())"""因sort是列表内置方法,所以字符串使用会报错"""
AttributeError:'str'object has no attribute 'sort'print(sorted(a))['a','b','e','f','g','l','o','s','u']
a ='abc'# 可以通过dir函数,查看它是否有iter方法
b = a.__iter__()# __iter__方法会返回迭代器(iterator)自身print(b.__next__())# __next__ 返回容器的下一个元素'a'print(b.__next__())'b'
b ='123'
b.__next__()# b是可迭代对象,但不是迭代器,所以没有next方法
AttributeError:'str'object has no attribute '__next__'
c = b.__iter__()# 调用iter方法, 返回迭代器print(c)<str_iterator object at 0x00000246E10AD5F8>
c.__next__()# 调用next, 返回容器的下一元素'1'
sorted和sort源码比较用法比较待更新源码比较sorted(iterable, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. 从包含新项的iterable列表中升序返回。 A custom key function can be supplied to customise the sort order, and the