Python:sample函數


sample(序列a,n)

功能:從序列a中隨機抽取n個元素,並將n個元素生以list形式返回。

例:

  • from random import randint, sample
    date = [randint(10,20) for _ in range(10)]
    c = sample(date, 5)
    print(c)
    # 輸出:[12, 17, 10, 12, 17]
  1. randint(10,20) for _ in range(10):從10~20間隨機抽取10個數;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM