目的:利用numpy中的数组计算一家三个孩子一个星期零花钱的使用情况。 条件:三个孩子每人每天的拿到的零花钱放在一个列表incoming中,每人每天所花费的零花钱放在另一个列表outgoing中。列表的每一行代表一个孩子,每一列代表一天。 要求计算(使用numpy模块,不使用循环语句): 每人的总收入和总支出。 家长每天的支出。 三个孩子每人每天储蓄罐中所剩钱数。 每人平均每天储蓄罐中所剩钱数。 一个星期后三个孩子总共所剩钱数。 提示:可用cumsum()函数
时间: 2023-05-12 10:03:48 浏览: 108
ChatGPT: 你可以使用numpy中的数组来计算这个问题。首先,你需要将incoming和outgoing列表转换为numpy数组。然后,你可以使用sum()函数来计算每个孩子的总收入和总支出,以及家长每天的支出。接下来,你可以使用cumsum()函数来计算每个孩子每天储蓄罐中所剩钱数,以及一个星期后三个孩子总共所剩钱数。最后,你可以使用mean()函数来计算每个孩子平均每天储蓄罐中所剩钱数。
以下是示例代码:
import numpy as np
# incoming和outgoing列表
incoming = [[10, 20, 30, 40, 50], [15, 25, 35, 45, 55], [20, 30, 40, 50, 60]]
outgoing = [[5, 10, 15, 20, 25], [10, 15, 20, 25, 30], [15, 20, 25, 30, 35]]
# 转换为numpy数组
incoming = np.array(incoming)
outgoing = np.array(outgoing)
# 每个孩子的总收入和总支出
total_income = np.sum(incoming, axis=1)
total_outgoing = np.sum(outgoing, axis=1)
# 家长每天的支出
daily_expense = np.sum(outgoing, axis=0)
# 每个孩子每天储蓄罐中所剩钱数
savings = incoming - outgoing
daily_savings = np.cumsum(savings, axis=1)
# 每个孩子平均每天储蓄罐中所剩钱数
avg_daily_savings = np.mean(daily_savings, axis=1)
# 一个星期后三个孩子总共所剩钱数
total_savings = np.sum(daily_savings[:, -1])
# 输出结果
print("每个孩子的总收入:", total_income)
print("每个孩子的总支出:", total_outgoing)
print("家长每天的支出:", daily_expense)
print("每个孩子每天储蓄罐中所剩钱数:", daily_savings)
print("每个孩子平均每天储蓄罐中所剩钱数:", avg_daily_savings)
print("一个星期后三个孩子总共所剩钱数:", total_savings)
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)