Python列表
代码如下
import random
list1=list(range(1,100,9))
print(list1)
#原地洗牌
random.shuffle(list1)
print("list1: ",list1)
list2=random.sample(list1,5)
print("list2: ",list2)
#返回一个int值
list3=random.choice(list2)
print("list3: ",list3)
#返回一个列表,包含一个int值
list4=random.choices(list2)
print("list4: ",list4)
#将元素加入列表尾部
list4.append(9)
print("list4: ",list4)
#全部加入列表尾部
list4.extend(list1)
print("list4: ",list4)
#插入元素
list4.insert(0,100)
print("list4: ",list4)
#删除一个元素
list4.remove(1)
print("list4: ",list4)
#原地排序
list4.sort(reverse=True)
print("list4: ",list4)
#非原地排序
print("list4: ",sorted(list4))
#删除尾部元素,并返回,类似出栈
print("list4: ",list4.pop())
执行结果如下
[1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91]
list1: [73, 55, 10, 1, 28, 46, 82, 19, 37, 91, 64]
list2: [64, 55, 46, 19, 28]
list3: 55
list4: [46]
list4: [46, 9]
list4: [46, 9, 73, 55, 10, 1, 28, 46, 82, 19, 37, 91, 64]
list4: [100, 46, 9, 73, 55, 10, 1, 28, 46, 82, 19, 37, 91, 64]
list4: [100, 46, 9, 73, 55, 10, 28, 46, 82, 19, 37, 91, 64]
list4: [100, 91, 82, 73, 64, 55, 46, 46, 37, 28, 19, 10, 9]
list4: [9, 10, 19, 28, 37, 46, 46, 55, 64, 73, 82, 91, 100]
list4: 9
Process finished with exit code 0