Python中的choice()方法使用详解
2015-05-15 11:17
1006 查看
choice()方法从一个列表,元组或字符串返回一个随机项。
语法
以下是choice()方法的语法:
choice( seq )
注意:此函数是无法直接访问的,所以我们需要导入random模块,然后我们需要使用random对象来调用这个函数。
参数
- 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')
当我们运行上面的程序,它会产生以下结果:
choice([1, 2, 3, 5, 9]) : 2 choice('A String') : n
您可能感兴趣的文章:
相关文章推荐
- Python中的choice()方法使用详解
- 举例详解Python中的split()函数的使用方法
- 详解Python中的__new__()方法的使用
- 详解Python中列表和元祖的使用方法
- python中的sort方法使用详解
- 详解Python中for循环的使用方法
- 详解Python中for循环的使用方法
- Python 3.x 字符串的makestrans函数和translate方法使用详解
- python BeautifulSoup使用方法详解
- python BeautifulSoup使用方法详解
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- python中的sort方法使用详解
- python中的sort方法使用详解
- 详解Python中find()方法的使用
- 详解Python中expandtabs()方法的使用
- 详解Python中expandtabs()方法的使用
- 详解Python中find()方法的使用
- Python中的rjust()方法使用详解
- Python中的rfind()方法使用详解