gpt4 book ai didi

python - 如何循环遍历 Python 列表并对列表中的元素执行数学计算?

转载 作者:行者123 更新时间:2023-12-01 08:29:09 25 4
gpt4 key购买 nike

我正在尝试创建一个合约桥赛点计分系统。在下面的列表中,第 1、第 3 等数字是配对编号(玩家),第 2、第 4 等数字是每对获得的分数。因此,第 2 组得分为 430,第 3 组得分为 420,依此类推。

我想循环遍历列表并按如下方式评分:

对于每组得分,第 2 组击败他们将获得 2 分,每打平则获得 1 分,如果他们没有击败,则获得 0 分。然后循环继续并以相同的方式比较每对的分数。在下面的示例中,第 2 组获得 7 分(击败其他 3 对,并以 1 平局),第 7 组获得 0 分,第 6 组击败其他所有对获得 12 分。

我的列表(从 elasticsearch json 对象生成)是:

['2', '430', '3', '420', '4', '460', '5', '400', '7', '0', '1', '430', '6', '480']

我尝试过的Python代码(经过多种变化)是:

nsp_mp = 0
ewp_mp = 0
ns_list = []
for row in arr["hits"]["hits"]:
nsp = row["_source"]["nsp"]
nsscore = row["_source"]["nsscore"]
ns_list.append(nsp)
ns_list.append(nsscore)

print(ns_list)
x = ns_list[1]
for i in range(6): #number of competing pairs
if x > ns_list[1::2][i]:
nsp_mp = nsp_mp + 2
elif x == ns_list[1::2][i]:
nsp_mp = nsp_mp
else:
nsp_mp = nsp_mp + 1
print(nsp_mp)

产生:

['2', '430', '3', '420', '4', '460', '5', '400', '7', '0', '1', '430', '6', '480']
7

根据上面的计算是正确的。但是当我尝试执行循环时,它不会返回正确的结果。

也许方法是错误的。这样做的正确方法是什么?

elasticsearch json 对象是:

arr = {'took': 0, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 7, 'max_score': 1.0, 'hits': [{'_index': 'match', '_type': 'score', '_id': 'L_L122cBjpp4O0gQG0qd', '_score': 1.0, '_source': {'tournament_id': 1, 'board_number': '1', 'nsp': '2', 'ewp': '9', 'contract': '3NT', 'by': 'S', 'tricks': '10', 'nsscore': '430', 'ewscore': '0', 'timestamp': '2018-12-23T16:45:32.896151'}}, {'_index': 'match', '_type': 'score', '_id': 'MPL122cBjpp4O0gQHEog', '_score': 1.0, '_source': {'tournament_id': 1, 'board_number': '1', 'nsp': '3', 'ewp': '10', 'contract': '4S', 'by': 'N', 'tricks': '10', 'nsscore': '420', 'ewscore': '0', 'timestamp': '2018-12-23T16:45:33.027631'}}, {'_index': 'match', '_type': 'score', '_id': 'MfL122cBjpp4O0gQHEqk', '_score': 1.0, '_source': {'tournament_id': 1, 'board_number': '1', 'nsp': '4', 'ewp': '11', 'contract': '3NT', 'by': 'N', 'tricks': '11', 'nsscore': '460', 'ewscore': '0', 'timestamp': '2018-12-23T16:45:33.158060'}}, {'_index': 'match', '_type': 'score', '_id': 'MvL122cBjpp4O0gQHUoj', '_score': 1.0, '_source': {'tournament_id': 1, 'board_number': '1', 'nsp': '5', 'ewp': '12', 'contract': '3NT', 'by': 'S', 'tricks': '10', 'nsscore': '400', 'ewscore': '0', 'timestamp': '2018-12-23T16:45:33.285460'}}, {'_index': 'match', '_type': 'score', '_id': 'NPL122cBjpp4O0gQHkof', '_score': 1.0, '_source': {'tournament_id': 1, 'board_number': '1', 'nsp': '7', 'ewp': '14', 'contract': '3NT', 'by': 'S', 'tricks': '8', 'nsscore': '0', 'ewscore': '50', 'timestamp': '2018-12-23T16:45:33.538710'}}, {'_index': 'match', '_type': 'score', '_id': 'LvL122cBjpp4O0gQGkqt', '_score': 1.0, '_source': {'tournament_id': 1, 'board_number': '1', 'nsp': '1', 'ewp': '8', 'contract': '3NT', 'by': 'N', 'tricks': '10', 'nsscore': '430', 'ewscore': '0', 'timestamp': '2018-12-23T16:45:32.405998'}}, {'_index': 'match', '_type': 'score', '_id': 'M_L122cBjpp4O0gQHUqg', '_score': 1.0, '_source': {'tournament_id': 1, 'board_number': '1', 'nsp': '6', 'ewp': '13', 'contract': '4S', 'by': 'S', 'tricks': '11', 'nsscore': '480', 'ewscore': '0', 'timestamp': '2018-12-23T16:45:33.411104'}}]}}

最佳答案

列表似乎是一个很糟糕的数据结构,我认为你通过扁平化你的elasticsearch对象让一切变得更糟。

Note there are a few minor mistakes in listings below - to make sure I'm not solving someone's homework for free. I also realize this is not the most efficient way of doing so.

尝试使用字典:

1)将您必须的elasticsearch json转换为具有更好结构的字典:

scores = {}
for row in arr["hits"]["hits"]:
nsp = row["_source"]["nsp"]
nsscore = row["_source"]["nsscore"]
scores[nsp] = nsscore

这会给你这样的东西:

{'1': '430',
'2': '430',
'3': '420',
'4': '460',
'5': '400',
'6': '480',
'7': '0'}

2)编写一个函数来计算配对得分:

def calculate_score(pair, scores):
score = 0
for p in scores:
if p == pair:
continue
if scores[p] < scores[pair]:
score += 2 # win
elif scores[p] == scores[pair]:
score += 1
return score

这应该给你这样的东西:

In [13]: calculate_score('1', scores)
Out[13]: 7

In [14]: calculate_score('7', scores)
Out[14]: 0

3) 循环所有对,计算分数。我将把这个作为练习。

关于python - 如何循环遍历 Python 列表并对列表中的元素执行数学计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54005909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com