总结2D列表 [英] Summing a 2D list
问题描述
大家好,
我有一个场景,我有这样一个列表:
用户评分
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
我需要将每个用户的分数加起来得到类似
的内容:
用户评分
1 6
2 4
3 2
4 8
这可能吗?如果是这样,我该怎么办?我已经尝试循环遍历
阵列并且到目前为止没有太多运气。
任何帮助非常感谢,
Mark
Hi all,
I have a scenario where I have a list like this:
User Score
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
And I need to add up the score for each user to get something like
this:
User Score
1 6
2 4
3 2
4 8
Is this possible? If so, how can I do it? I''ve tried looping through
the arrays and not had much luck so far.
Any help much appreciated,
Mark
推荐答案
Mark写道:
Mark wrote:
大家好,
我有一个场景,我有这样的列表:
用户评分
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
我需要将每个用户的分数加起来得到类似
这样的东西:
用户评分
1 6
2 4 >
3 2
4 8
这可能吗?如果是这样,我该怎么办?我已经尝试循环遍历
阵列并且到目前为止没有太多运气。
任何帮助非常感谢,
Hi all,
I have a scenario where I have a list like this:
User Score
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
And I need to add up the score for each user to get something like
this:
User Score
1 6
2 4
3 2
4 8
Is this possible? If so, how can I do it? I''ve tried looping through
the arrays and not had much luck so far.
Any help much appreciated,
告诉我们您目前为止在代码方面的努力。特别是实际数据看起来像是b $ b。然后我们可以建议一个解决方案。
Diez
Show us your efforts in code so far. Especially what the actual data looks
like. Then we can suggest a solution.
Diez
6月12日下午3:48 *,Mark< markjtur。 .. @ gmail.comwrote:
On Jun 12, 3:48*pm, Mark <markjtur...@gmail.comwrote:
大家好,
我有一个场景,我有这样的列表:
用户* * * * * *分数
1 * * * * * * * * 0
1 * * * * * * * * 1
1 * * * * * * * * 5
2 * * * * * * * * 3
2 * * * * * * * * 1
3 * * * * * * * * 2
4 * * * * * * * * 3
4 * * * * * * * * 3
4 * * * * * * * * 2
我需要加起来每个用户的分数得到类似
这个:
用户* * * * * *分数
1 * * * * * * * * 6
2 * * * * * * * * 4
3 * * * * * * * * 2
4 * * * * * * * * 8
这可能吗?如果是这样,我该怎么办?我已经尝试循环遍历
阵列并且到目前为止没有太多运气。
任何帮助非常感谢,
马克
Hi all,
I have a scenario where I have a list like this:
User * * * * * *Score
1 * * * * * * * * 0
1 * * * * * * * * 1
1 * * * * * * * * 5
2 * * * * * * * * 3
2 * * * * * * * * 1
3 * * * * * * * * 2
4 * * * * * * * * 3
4 * * * * * * * * 3
4 * * * * * * * * 2
And I need to add up the score for each user to get something like
this:
User * * * * * *Score
1 * * * * * * * * 6
2 * * * * * * * * 4
3 * * * * * * * * 2
4 * * * * * * * * 8
Is this possible? If so, how can I do it? I''ve tried looping through
the arrays and not had much luck so far.
Any help much appreciated,
Mark
user_score = {}
列表记录:
user,score = record.split()
如果用户在user_score中:user_score [user] + =得分
else:user_score [user] =得分
print''\ n''。join([''%s \ t%s'''%(用户,得分)为用户,得分为
排序(user_score.items) ())])
你没有提到你在
中保存记录的数据结构,但希望这可以帮助你朝着正确的方向前进。
user_score = {}
for record in list:
user, score = record.split()
if user in user_score: user_score[user] += score
else: user_score[user] = score
print ''\n''.join([''%s\t%s'' % (user, score) for user,score in
sorted(user_score.items())])
You don''t mention what data structure you are keeping your records in
but hopefully this helps you in the right direction.
6月12日,3:02 * pm,Diez B. Roggisch < de ... @nospam.web.dewrote:
On Jun 12, 3:02*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Mark写道:
Mark wrote:
大家好,
Hi all,
我有一个场景,我有这样的列表:
I have a scenario where I have a list like this:
用户* * * * * *分数
1 * * * * * * * * 0
1 * * * * * * * * 1 >
1 * * * * * * * * 5
2 * * * * * * * * 3
2 * * * * * * * * 1
3 * * * * * * * * 2
4 * * * * * * * * 3
4 * * * * * * * * 3
4 * * * * * * * * 2
User * * * * * *Score
1 * * * * * * * * 0
1 * * * * * * * * 1
1 * * * * * * * * 5
2 * * * * * * * * 3
2 * * * * * * * * 1
3 * * * * * * * * 2
4 * * * * * * * * 3
4 * * * * * * * * 3
4 * * * * * * * * 2
我需要加上得分为每个用户得到类似
这个:
And I need to add up the score for each user to get something like
this:
用户* * * * * *得分
1 * * * * * * * * 6
2 * * * * * * * * 4
3 * * * * * * * * 2
4 * * * * * * * * 8
User * * * * * *Score
1 * * * * * * * * 6
2 * * * * * * * * 4
3 * * * * * * * * 2
4 * * * * * * * * 8
这可能吗?如果是这样,我该怎么办?我已经尝试循环遍历
数组,到目前为止没有多少运气。
Is this possible? If so, how can I do it? I''ve tried looping through
the arrays and not had much luck so far.
任何帮助非常感谢,
Any help much appreciated,
向我们展示您目前为止的代码工作。特别是实际数据看起来像是b $ b。然后我们可以建议一个解决方案。
Diez
Show us your efforts in code so far. Especially what the actual data looks
like. Then we can suggest a solution.
Diez
您好Diez,感谢您的快速回复。
说实话,我对Python比较陌生,所以我不太了解所有循环结构如何工作以及它们与其他结构有何不同 br />
语言。我正在Django构建一个应用程序,这个数据正在出现
的数据库,看起来就像我放在那里的那样!
这是我的(失败的)尝试:
预测= Prediction.objects.all()
得分= []
用于预测预测:
i = [prediction.predictor.id,0]
如果forecast.predictionscore:
i [1] + = int(预测。预测核心)
scores.append(i)
我确实有另一个循环(我很确定我需要一个)但是那个
也没办法。我不觉得这个片段是非常有用的,
对不起!
任何提示都会感激不尽!
谢谢,
马克
Hi Diez, thanks for the quick reply.
To be honest I''m relatively new to Python, so I don''t know too much
about how all the loop constructs work and how they differ to other
languages. I''m building an app in Django and this data is coming out
of a database and it looks like what I put up there!
This was my (failed) attempt:
predictions = Prediction.objects.all()
scores = []
for prediction in predictions:
i = [prediction.predictor.id, 0]
if prediction.predictionscore:
i[1] += int(prediction.predictionscore)
scores.append(i)
I did have another loop in there (I''m fairly sure I need one) but that
didn''t work either. I don''t imagine that snippet is very helpful,
sorry!
Any tips would be gratefully recieved!
Thanks,
Mark
这篇关于总结2D列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!