(1)输入一个正整数n,自动生成n个1~100范围内的随机浮点数,输出每个随机数,计算并显示平均值。输入/输出示例如表8-25所示。 表 8-25 输入/输出示例 输 入 输 出 27.337682138808397 25.469857251321084 4 86.76520259704735 3.68117383527287464 the average is:35.81362008 (2)创建任意一个包含完整年、月、日、时、分、秒的datetime对象,计算这个datetime的时间戳,将时间戳除以86400,并计算datetime对象与1970年01月01日00时00分00秒的时间差。 (3)使用元字符匹配“张三和李四的出生日期分别是1999-07-02和1998-05-17”中的时间字符串。
时间: 2024-04-12 07:27:41 浏览: 37
(1) 以下是生成随机浮点数并计算平均值的代码:
```python
import random
def generate_random_floats(n):
random_floats = []
total = 0
for _ in range(n):
random_float = random.uniform(1, 100)
random_floats.append(random_float)
total += random_float
average = total / n
return random_floats, average
# 输入n
n = int(input("请输入一个正整数n:"))
random_floats, average = generate_random_floats(n)
# 输出每个随机数
for random_float in random_floats:
print(random_float)
# 输出平均值
print("the average is:", average)
```
(2) 以下是创建datetime对象、计算时间戳和时间差的代码:
```python
import datetime
# 创建datetime对象
dt = datetime.datetime(2022, 1, 1, 12, 0, 0)
# 计算时间戳
timestamp = dt.timestamp()
# 计算与1970年01月01日00时00分00秒的时间差
time_diff = dt - datetime.datetime(1970, 1, 1, 0, 0, 0)
# 输出时间戳和时间差
print("时间戳:", timestamp)
print("时间差:", time_diff)
```
(3) 以下是使用元字符匹配时间字符串的代码:
```python
import re
text = "张三和李四的出生日期分别是1999-07-02和1998-05-17"
matches = re.findall("\d{4}-\d{2}-\d{2}", text)
for match in matches:
print(match)
```
请注意,以上代码示例是使用Python编写的。你可以在本地的Python环境中运行这些代码来获得相应的输出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)