Python choice()返回列表、元组或字符串中的随机项。
choice( seq )
seq - 这可能是列表、元组或字符串。
此方法返回一个随机项。
下面的示例显示choice()方法的用法。
#!/usr/bin/python import random print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]) print "choice('A String') : ", random.choice('A String')
当无涯教程运行上面的程序时,它产生以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/python/number-choice.html
来源:LearnFk无涯教程网
choice([1, 2, 3, 5, 9]) : 2 choice('A String') : n
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)