下列选项中,能够最简单的在列表['apple','pear','peach','orange']中随机选取一个元素的是( )。
时间: 2023-08-06 12:21:53 浏览: 294
使用Python内置的random模块中的choice函数可以最简单地在列表中随机选取一个元素。示例代码如下:
```
import random
fruits = ['apple', 'pear', 'peach', 'orange']
random_fruit = random.choice(fruits)
print(random_fruit)
```
运行结果可能为:
```
peach
```
相关问题
随机选取列表['apple', 'pear', 'peach', 'orange']中的一个字符串。
可以使用Python中的random模块中的choice函数来实现:
```python
import random
fruits = ['apple', 'pear', 'peach', 'orange']
selected_fruit = random.choice(fruits)
print(selected_fruit)
```
输出结果为:
```
apple
```
每次运行程序,选取的水果都有可能不同。
(4)随机选取列表['apple','pear','peach','orange']中的1个字符串
好的,我来为您生成一个随机选取的字符串:
```python
import random
fruits = ['apple', 'pear', 'peach', 'orange']
random_fruit = random.choice(fruits)
print("随机选取的水果是:", random_fruit)
```
输出结果可能为:
```
随机选取的水果是: peach
```