~玉米糊~
慢慢来,也会很快。 非宁静无以志学,学什么都一样,慢慢打基础,找规律、认真、坚持,其余的交给时间。
随笔 - 117,  文章 - 17,  评论 - 1,  阅读 - 81778

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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. 如何对列表元素进行随机排列

1
2
3
a = [1,2,3,4,5,6,7,8,9,0]
random.shuffle(a)
print(a)
posted on   yuminhu  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示