生成随机列表需要导入random,循环生成随机数,添加到列表里面
import random
s= []
for x in range(0, 20):
s1=random. randint(1, 101)
s. append (s1)
print(s)
用循环切片的方法,把下标为偶数的取出来,放到另一个列表里面
sq=[]
for x1 in range(0, 20, 2):
sq. append(s[x1])
对另一个列表进行相关排序
sq. sort(reverse=True)
把另一个列表的值,赋值给第一个列表的偶数下标,输出
for asd in range(0, 10):
m1 =asd*2
s[m1]=sq [asd]
print(s)
结果随机:
[42, 34, 74, 35, 23, 48, 16, 72, 56, 4, 36, 45, 76, 78, 19, 73, 97, 22, 70, 6]
[97, 34, 76, 35, 74, 48, 70, 72, 56, 4, 42, 45, 36, 78, 23, 73, 19, 22, 16, 6]
所有代码:
s= []
sq=[]
for x in range(0, 20):
s1=random. randint(1, 101)
s. append (s1)
print(s)
for x1 in range(0, 20, 2):
sq. append(s[x1])
sq. sort(reverse=True)
for asd in range(0, 10):
m1 =asd*2
s[m1]=sq [asd]
print(s)