美文网首页
有关python2、python3里面的sorted()和lis

有关python2、python3里面的sorted()和lis

作者: salix_ | 来源:发表于2020-02-04 16:26 被阅读0次

主要是说python3,现在都0202年了,大清早亡了。

一:sorted:

python3中取消了cmp参数

1:sorted不改变原序列的顺序,只是返回一个排序之后的序列结果。
2:python3:

sorted(iterable, key=None, reverse=False)
当带排序列表的元素由多字段构成时,我们可以通过sorted(iterable ,key ,reverse)的参数key来制定我们更具那个字段对列表进行排序。(看下面代码的sorted,我把reverse这个参数省略了)

#导入cmp_to_key
from functools import cmp_to_key
def MyCmp(x,y):
    if(x+y<y+x):
        return 1
    else:
        return -1
n=int(input())
s=list(map(str,input().split(" ")))
s=sorted(s,key=cmp_to_key(MyCmp))
for temp in s:
    print(temp,end="")
print()
python2

sorted(iterable, cmp=None, key=None, reverse=False)

比较:python3 key可以当作python2中的cmp和key使用

二:list.sort()

1:list.sort是直接在原来列表上修改
2:比较
python3

list.sort( key=None, reverse=False)

python2

list.sort(cmp=None, key=None, reverse=False)

python3同样是去掉了python2中的cmp

相关文章

网友评论

      本文标题:有关python2、python3里面的sorted()和lis

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