sort()、reverse() 是列表的方法,直接对原列表进行操作。sorted() 是函数,其返回的是一个新的列表。===difang=['d','c','b','a']difang.sort()difang.sort(reverse=True)x=sorted(difang,reverse=True)print(difang)print(x)
类似地,如果我们对字符串调用reverse()方法的话则会报错,为'str' object has no attribute 'reverse'。2.4 使用sorted函数 sorted() 函数可以对可迭代对象进行排序,而且还可以指定排序规则。当我们将可迭代对象按照相反的顺序排序时,即可实现逆序输出。例如: my_list = [1, 2, 3, 4, 5]reversed_list =...
TypeError:'range'objectisnotan iterator>>>I1 =iter(R)>>>next(I1)0>>>next(I1)1>>>I2 =iter(R)>>>next(I2)0>>> zip, map, filter不支持相同结果上的多个活跃迭代器 >>>z =zip([1,2,3],['a','b','c'])>>>list(z) [(1,'a'), (2,'b'), (3,'c')]>>>I1 =iter(z...
2、sorted() 作用于任意可迭代的对象,而 sort()一般作用于列表。 >>> a = (1,2,4,2,3) >>> a.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'sort' >>> sorted(a) [1, 2, 2, 3, 4] 下面的例...
Python内建的list.sort()方法和sorted()函数都可以实现对列表进行排序。 一、list.sort()方法:list.sort(key=function, reverse=Boolean) list.sort()方法是对列表list直接进行排序,排序完成后原来的list列表中的元素位置变化,按排序顺序排列。 可选的关键字参数reverse为布尔型数据,设置排序方向,默认值是False,按照...
AttributeError: 'tuple' object has no attribute 'sort' >>> values_to_sort = list(tuple_val) >>> returned_from_sort = values_to_sort.sort() >>> print(returned_from_sort) None >>> print(values_to_sort) [1, 3, 5, 5] When you try calling .sort() with a tuple, you get an...
AttributeError:'dict'objecthas no attribute'sort' 而sorted则不然。看样例: >>>b_dict {1:'e',3:'m',5:'e',9:'a'}>>>sorted(b_dict) [1,3,5,9] sorted之后。上述对dictinoary中,将key值取出并排序,返回list类型的排序结果。 依照指定关键词排序 ...
>>>my_list.sorted()#AttributeError:'list'object has no attribute'sorted'>>>my_dict={'host':'http://bbs.fishc.com','port':'80'}>>>print(my_dict['server'])#不存在的“键”引发 KeyError:'server'异常 f=open('C:\\test.txt',wb)#注意open()第二个参数是字符串 否则引发 NameError ...
no attribute 'sort'>>>_=sorted({2: 'two', 0: 'zero', 1: 'one'})>>># sort a set>>>_=set([2, 3, 4]).sort()Traceback (most recent call last): File "<stdin>", line 1, in<module>AttributeError: 'set' object has no attribute 'sort'>>>_=sorted(set([2, 3, 4])) ...
numbers = 1, 2, 3isinstance(numbers, list)Trueisinstance(numbers, str)False 也可以把多个类型放在元组中,其中一个与对象的类型相符即为True,若无相符则为False。如: numbers = 1, 2, 3isinstance(numbers, (list, str))True dir()示例: dir(list) ’__add__’, ‘__class__’, ‘__contains__...