python(列表2)

时间:2024-01-04 22:11:14

1.remove(删除指定值的元素)

x = ['to','be','or','not','to','be']

x.remove('be')

x

['to','or','not','to','be']

2.reverse(按照相反的顺序排列列表中的元素)

s = [1,2,3]

s.reverse()

s

[3,2,1]

3.sort(按顺序排列)

x = [2,3,4,5,7,9,6,0]

x.sort()

x

[0,2,3,4,5,6,7,9]

sorted函数

x = [4,6,2,1,7,9]

y = sorted(x)

x

[4,6,2,1,7,9]

y

[1,2,4,6,7,9]

4.高级排序:方法sort接受两个可选参数:key和reverse

x = ['aardfgasgas','sasasasas','a','aaa']

x.sort(key = len)按照长度来排序

x

['a','aaa','sasasasas','aardfgasgas']

x = [4,6,5,7,2,1]

x.sort(reverse=True)指定一个值true or false,是否按照相反的顺序排序

x

[1,2,4,5,6,7]