import random List = [ random.randint(0,100) for i in range(20) ] a = sorted(List[:10]) b = sorted(List[10:],reverse=True) List = a + b print(List)# 写法2: sort()函数。 直接改变原来的列表
''' sort()函数只允许排序整个列表,不允许用切片操作排序列表的部分元素!!! ''' import random List = [ random.randint(0,100) for i in range(20) ] a = List[:10] a.sort() b = List[10:] b.sort(reverse=True) List = a + b print(List)
import random a = [random.randint(0, 100) for i in range(20) ] b = sorted(a[:10])+sorted(a[10:], reverse=True)
import random list_random=[] for i in range(20): list_random.append(random.randint(0,100)) print(sorted(list_random[:10])) print(sorted(list_random[10:],reverse=True))