python - Aggregating results from nested tuples based on conditions
问题描述
i have a nested tuple and i want to get a table with results, i'm trying to use comprehension on this but i'm not getting the best results.
team = ((35, 'Team1'),(8, 'Team2'),(55, 'Team1'),(65, 'Team2'),(90, 'Team1'))
output example:
first second totalgoals
team1 1 2 3
team2 1 1 2
Can anyone give me some help?
解决方案
您可以使用字典理解:
team = ((35, 'Team1'),(8, 'Team2'),(55, 'Team1'),(65, 'Team2'),(90, 'Team1'))
results = {a:[sum(c < 45 and d == a for c, d in team), sum(c >= 45 and d == a for c, d in team)] for a in set(d for _, d in team)}
print('\tfirst second totalgoals')
for a, [first, second] in results.items():
print(f'{a}\t{first}\t{second}\t{first+second}')
输出:
first second totalgoals
Team1 1 2 3
Team2 1 1 2
推荐阅读
- c# - 如何在 linq DateTime 中正确设置变量?
- adobe - 如何正确表示/缩放从 Adobe XD 到 aframe 的坐标?
- javascript - array.includes() 方法在检查另一个数组中是否存在数组/对象时给出 false 是什么?
- android - 当我在主活动中使用开始按钮访问登录活动时,为什么我的应用程序会崩溃?
- haskell - 一般而言,单子变换器是由附加词产生的吗?
- c# - 用于 C# 应用程序中的多个客户端的 MSMQ
- wordpress - Facebook - 卷曲错误:WRITE_ERROR 写入正文失败
- python - Twitter API - Tweepy 库 - send_direct_message
- php - mysqli 从单个 sql 表列返回 json 数组
- swift - 是否可以在 SwiftUI 中使用带有自定义字体的动态字体大小?