python - 如何对元组求和?因为原始总和将它们连接起来
问题描述
我有一个很大的字典,我正在其中扫描一个 API,并根据曲棍球比赛中的投篮是否是目标来添加值。这是我现在的代码...
for key in contents['liveData']['plays']['allPlays']:
# for plays in key['result']['event']:
# print(key)
if (key['result']['event'] == "Shot"):
#print(key['result']['event'])
scoordinates = (key['coordinates']['x'], key['coordinates']['y'])
if scoordinates not in shots:
shots[scoordinates] = (1, 0)
else:
shots[scoordinates] += (1, 0)
if (key['result']['event'] == "Goal"):
#print(key['result']['event'])
gcoordinates = (key['coordinates']['x'], key['coordinates']['y'])
if gcoordinates not in shots:
shots[gcoordinates] = (1, 1)
else:
shots[gcoordinates] += (1, 1)
我试图通过使用括号向字典添加两个值,但这不起作用,因为每当有重复的坐标拍摄时,表格都会添加这样的值 (1,0, 1, 1) 而不是 (2 ,1)。这可能吗?我在想这里可能有一个简单的格式修复,但我不完全确定。
解决方案
您可以使用 sum 代替 +=:
a = tuple(map(sum, zip(a, b)))
因此,对于这种情况,请使用:
shots[gcoordinates] = tuple(map(sum, zip((1, 1), shots[gcoordinates])))
推荐阅读
- android - 在 Android 中使用 MediaRecorder 记录整个屏幕并适合任何手机分辨率
- docker - 测试服务器拒绝 Meraki API 连接
- crash - Minecraft Foge 崩溃:java.lang.ArrayIndexOutOfBoundsException:0
- javascript - 点击动画缩放
- javascript - 当按多个数据属性对 div 进行排序时,如何指定 ASC / DESC?
- java - 必须在排序数组中打印频率。我的解决方案有错误,如何解决?
- azure-devops - 导航到 Azure Devops 中的 Markdown 文件区域
- sql - SQL - 根据预定义的拆分数量创建类别数量
- swift - 快速从 WKNavigationResponse 获取数据
- node.js - 如何为未登录的用户设置 cookie 以在 react 和 nodejs 中从数据库中获取购物车值