首先,如果问题太长,我很抱歉。我试图包含最少的数据,但那是我可以添加的。
这些是我的清单:
products_income = [
{'client_group': 'A', 'count': 2, 'product': 'A', 'monthly_income': 370},
{'client_group': 'B', 'count': 1, 'product': 'B', 'monthly_income': 215},
{'client_group': 'C', 'count': 3, 'product': 'C', 'monthly_income': 495},
{'client_group': 'A', 'count': 2, 'product': 'D', 'monthly_income': 304},
{'client_group': 'B', 'count': 1, 'product': 'E', 'monthly_income': 110},
{'client_group': 'B', 'count': 2, 'product': 'F', 'monthly_income': 560},
{'client_group': 'A', 'count': 1, 'product': 'G', 'monthly_income': 196},
]
client_package = [
{'client_group': 'A', 'total_income': 870},
{'client_group': 'B', 'total_income': 885},
{'client_group': 'C', 'total_income': 495}
]
client_group_user_counts = {
'A': ['user1', 'user2', 'user3', 'user4', 'user5'], # 5 users
'B': ['user21', 'user22', 'user23', 'user24'], # 4 users
'C': ['user41', 'user42', 'user43'], # 3 users
}
这些是我商店的输出,这是写入“ client_group
.txt”调用的不同文件的预期输出:
A.txt
:
group A has 2 product A, monthly income is 370.
group A has 2 product D, monthly income is 304.
group A has 1 product G, monthly income is 196.
group A total income is 870.
group A has total 5 users, and this is its users:
user1
user2
user3
user4
user5
B.txt
:
group B has 1 product B, monthly income is 215.
group B has 1 product E, monthly income is 110.
group B has 2 product F, monthly income is 560.
group B total income is 885.
group B has total 4 users, and this is its users
user21
user22
user23
user24
C.txt
:
group C has 3 product C, monthly income is 495.
group C total income is 495.
group C has total 3 users, and this is its users:
user41
user42
user43
这是我当前的代码,我尚未达到预期的输出(实际上,除了我的代码的其他问题之外,目前我不知道如何在这种情况下写入单独的文件):
# I added this function, because I had error in the last line of next code block inside the `f-string`, so I thought that could be a workaround for this
def to_join():
return '\n'.join(client_group_user_counts[user])
for product in products_income:
for client in client_package:
for user in client_group_user_counts:
if product['client_group'] == client['client_group'] == user:
print(f"""group {product['client_group']} has {product['count']} product {product['product']}, monthly income is {product['monthly_income']}.
total income is {client['total_income']}.
group {product['client_group']} has total {len(client_group_user_counts[user])} users, and this is its users:
{to_join()}
""")
这是当前输出:
group A has 2 product A, monthly income is 370.
group A total income is 870.
group A has total 5 users, and this is its users:
user1
user2
user3
user4
user5
group B has 1 product B, monthly income is 215.
group B total income is 885.
group B has total 4 users, and this is its users:
user21
user22
user23
user24
group C has 3 product C, monthly income is 495.
group C total income is 495.
group C has total 3 users, and this is its users:
user41
user42
user43
group A has 2 product D, monthly income is 304.
group A total income is 870.
group A has total 5 users, and this is its users:
user1
user2
user3
user4
user5
group B has 1 product E, monthly income is 110.
group B total income is 885.
group B has total 4 users, and this is its users:
user21
user22
user23
user24
group B has 2 product F, monthly income is 560.
group B total income is 885.
group B has total 4 users, and this is its users:
user21
user22
user23
user24
group A has 1 product G, monthly income is 196.
group A total income is 870.
group A has total 5 users, and this is its users:
user1
user2
user3
user4
user5
逻辑如下:
您已经拥有 中的所有组名称
client_group_user_counts
。我们将使用它来生成一堆文件。我们还将把组名映射到文件名(这个程序并不完全需要,但这是一个很好的选择,以防万一您的逻辑在以后变得更加复杂)。然后,我们将迭代
products_income
. 我们使用值查找输出文件名group_name
并写入格式化的记录。一旦我们写完所有记录,我们将迭代
client_package
写出总收入。但是等一下,我们将重新格式化该字典,以便键是组名称,值是总收入 - 这只会节省我们一些查找步骤。最后,我们将迭代
client_group_user_counts
。由于键已经是组名称,因此我们可以使用它们来查找输出文件。然后我们只需要将用户名本身写入输出文件这是代码:
我会首先重新编写字典,以避免以后忽略它们。
然后对每个键仅循环一次并使用以下语句打开文件
with
:例子
A.txt
:由于结果是按“组 ID”聚合的,因此您需要
client_group_user_counts
首先进行迭代。这还允许您获取open()
特定于当前“组 ID”的文件这可能需要一些重构,但它应该给你一个想法
鉴于:
尝试:
不知道我是否理解你的程序结构,如果我错了,请纠正我的。由于您拥有组,因此
client_group_user_counts
您可以从其键中获取所有组。现在您已经拥有了所有组,您可以迭代组并打印上面提到的内容。
但通常建议使用 pandas 等库来完成更复杂的任务。