随手小代码——Python 从集合中随机抽取元素

时间:2023-03-09 03:30:45
随手小代码——Python 从集合中随机抽取元素

=================================版权声明=================================

版权声明:原创文章 谢绝转载 

请通过右侧公告中的“联系邮箱(wlsandwho@foxmail.com)”联系我

勿用于学术性引用。

勿用于商业出版、商业印刷、商业引用以及其他商业用途。                

本文不定期修正完善。

本文链接:http://www.cnblogs.com/wlsandwho/p/8539169.html

耻辱墙:http://www.cnblogs.com/wlsandwho/p/4206472.html

=======================================================================

从集合中随机抽取元素(不放回)

 import math
from numpy import * dataIndex=list(range(20))
print(dataIndex)
print('----------')
for i in range(20):
randIndex =random.choice(dataIndex) # go to 0 because of the constant
print(randIndex)
del(dataIndex[dataIndex.index(randIndex)])
print(dataIndex)
print('----------')
print('\n')

运行结果

"C:\Program Files\Python36-32\python.exe" C:/PythonResearch/LogisticRegression/Test_logRegres.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
----------
15
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19]
----------
8
[0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19]
----------
14
[0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 16, 17, 18, 19]
----------
4
[0, 1, 2, 3, 5, 6, 7, 9, 10, 11, 12, 13, 16, 17, 18, 19]
----------
3
[0, 1, 2, 5, 6, 7, 9, 10, 11, 12, 13, 16, 17, 18, 19]
----------
10
[0, 1, 2, 5, 6, 7, 9, 11, 12, 13, 16, 17, 18, 19]
----------
6
[0, 1, 2, 5, 7, 9, 11, 12, 13, 16, 17, 18, 19]
----------
9
[0, 1, 2, 5, 7, 11, 12, 13, 16, 17, 18, 19]
----------
16
[0, 1, 2, 5, 7, 11, 12, 13, 17, 18, 19]
----------
19
[0, 1, 2, 5, 7, 11, 12, 13, 17, 18]
----------
7
[0, 1, 2, 5, 11, 12, 13, 17, 18]
----------
0
[1, 2, 5, 11, 12, 13, 17, 18]
----------
13
[1, 2, 5, 11, 12, 17, 18]
----------
2
[1, 5, 11, 12, 17, 18]
----------
1
[5, 11, 12, 17, 18]
----------
12
[5, 11, 17, 18]
----------
18
[5, 11, 17]
----------
5
[11, 17]
----------
17
[11]
----------
11
[]
---------- Process finished with exit code 0