我有以下列表:
cnames = [" green ", " blue ", " yellow ", " gray ", " pink ", " orange ", "purple ", " red ", "brown "]
如何从该列表中获取6个随机且唯一的索引,表示0
和len(cnames)
之间的数字?
答案 0 :(得分:2)
您可以使用python标准库<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">
<div id="mywrap">
<img class="getimage" src="http://icons.iconarchive.com/icons/umut-pulat/tulliana-2/128/k-get-icon.png"/><br />
<img class="sinistra" src="http://www.bicielettriche.bikeitalia.it/wp-content/uploads/2014/11/bici-elettrica-piaggio.png"/>
</div>
<br /><br /><br />
<div id="mywrap">
<img class="getimage" src="http://icons.iconarchive.com/icons/umut-pulat/tulliana-2/128/k-get-icon.png"/><br />
<img class="sinistra" src="https://eradellabicicletta.files.wordpress.com/2013/01/bici-da-corsa-rb1000-team-edition.jpg"/><br />
</div>
</div>
及其函数sample
:
random
答案 1 :(得分:1)
您可以在列表中使用import random
print(random.sample(range(len(cnames)), 6))
方法和random.shuffle(list)
方法。这是一个例子:
pop()