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


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

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

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

    勿用于学术性引用。

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

    本文不定期修正完善。

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

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

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

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

     1 import math
     2 from numpy import *
     3 
     4 
     5 dataIndex=list(range(20))
     6 print(dataIndex)
     7 print('----------')
     8 for i in range(20):
     9     randIndex =random.choice(dataIndex)  # go to 0 because of the constant
    10     print(randIndex)
    11     del(dataIndex[dataIndex.index(randIndex)])
    12     print(dataIndex)
    13     print('----------')
    14 print('
    ')

    运行结果

    "C:Program FilesPython36-32python.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
  • 相关阅读:
    BCB编写DLL终极手册
    C++Builder常用函数
    c++与js脚本交互,C++调用JS函数JS调用C++函数
    JS调用C++
    c&c++MFC 调用 js 函数代码
    C++调用JS,JS调用C++
    MFC基础类及其层次结构
    MFC文档视图结构学习笔记
    深入理解JVM虚拟机1:JVM内存的结构与永久代的消失
    深入理解JVM虚拟机开篇:JVM介绍与知识脉络梳理
  • 原文地址:https://www.cnblogs.com/wlsandwho/p/8539169.html
Copyright © 2020-2023  润新知