a ={34,54,1,2,3,56,453,4563,4}len(a)max(a)min(a)list(a)str(a)sum(a)
a ={34,54,1,2,3,56,453,4563,4}# sorted(a)# reversed(a)for i inenumerate(a):print(i)(0,1)(1,34)(2,3)(3,2)(4,453)(5,4)(6,4563)(7,54)(8,56)
2. 集合没有sort()方法,sorted()python内置函数不能用于集合
a ={1,2,3}
a.sorted()---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)<ipython-input-29-b09d0a76cf4f>in<module>1 a ={1,2,3}---->2 a.sorted()
AttributeError:'set'object has no attribute 'sorted'
a ={74872,42847,6,1,2,3}
a.sort()---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)<ipython-input-29-b09d0a76cf4f>in<module>1 a ={1,2,3}---->2 a.sorted()
AttributeError:'set'object has no attribute 'sorted'
3. list的reverse()、sort()属性方法 VS 对list使用python内置函数reversed()、sort()
内置函数或方法
功能
使用
返回值
是否改变list
reverse()
list的方法
list.reverse()
None
是
sort()
list的方法
list.sort()
None
是
reversed
python 内置函数
reversed(list)
list_reverseiterator object
否
sorted
python内置函数
sorted(list)
list object
否
a =[84457,-332,1,2,3,-7762,73]print(sorted(a))print(a)print(reversed(a))print(a)for i inreversed(a):print(i)# 不改变原列表[-7762,-332,1,2,3,73,84457][84457,-332,1,2,3,-7762,73]<list_reverseiterator object at 0x00000249FC6C8048>[84457,-332,1,2,3,-7762,73]73-7762321-33284457
a =[84457,-332,1,2,3,-7762,73]print(a.sort())print(a)print(a.reverse())print(a)# 直接对列表原地a操作None[-7762,-332,1,2,3,73,84457]None[84457,73,3,2,1,-332,-7762]