~玉米糊~
慢慢来,也会很快。 非宁静无以志学,学什么都一样,慢慢打基础,找规律、认真、坚持,其余的交给时间。

1 . 编写一个函数(不要使用python模块的函数),打乱列表元素的顺序

import random

a = [1,2,3,4,5,6,7,8,9, 0]
def random_list1(a):
    for i in range(0, 100):
        index1 = random.randint(0, len(a) - 1)
        index2 = random.randint(0, len(a) - 1)
        a[index1], a[index2] = a[index2], a[index1]
    return a

b = random_list1(a)
print(b)

def random_list2(a):
    a_copy = a.copy()
    result = []
    count = len(a)
    for i in range(0, count):
        index = random.randint(0, len(a_copy) - 1)
        result.append(a_copy[index])
        del a_copy[index]
    return  result

a = [1,2,3,4,5,6,7,8,9,0]
b = random_list2(a)
print(b)

  

 

2. 如何对列表元素进行随机排列

a = [1,2,3,4,5,6,7,8,9,0]
random.shuffle(a)
print(a)
posted on 2022-04-16 20:42  yuminhu  阅读(49)  评论(0编辑  收藏  举报