Python 随机获取数组、字符串、字典
时间: 2024-01-07 12:05:27 浏览: 134
Python怎样输出随机字符串.txt
获取随机数组:
```python
import random
my_list = [1, 2, 3, 4, 5]
random_element = random.choice(my_list)
print(random_element)
```
获取随机字符串:
```python
import random
import string
my_string = "Hello, World!"
random_char = random.choice(my_string)
print(random_char)
```
获取随机字典键:
```python
import random
my_dict = {"a": 1, "b": 2, "c": 3}
random_key = random.choice(list(my_dict.keys()))
print(random_key)
```
阅读全文