-1
我想弄清楚2D数组是如何工作的,如果有人可以向我解释如何使用二维数组编码一个表,其中有一个列中有团队1和2玩家在第二栏中有一个积分值。一旦我完成了这些,我需要能够将球队1的成绩加起来,并且让球队2的成绩上升。共有20名球员。Python 2D阵列团队表
谢谢!
我想弄清楚2D数组是如何工作的,如果有人可以向我解释如何使用二维数组编码一个表,其中有一个列中有团队1和2玩家在第二栏中有一个积分值。一旦我完成了这些,我需要能够将球队1的成绩加起来,并且让球队2的成绩上升。共有20名球员。Python 2D阵列团队表
谢谢!
看看下面的例子:
# Two 1D arrays
team1_scores = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
team2_scores = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
# This is your 2D array
scores = [ team1_scores, team2_scores ]
# You can sum an array using pythons built-in sum method
for idx, score in enumerate(scores):
print 'Team {0} score: {1}'.format(idx+1, sum(score))
# Add a blank line
print ''
# Or you can manually sum each array
# Iterate through first dimension of array (each team)
for idx, score in enumerate(scores):
team_score = 0
print 'Summing Team {0} scores: {1}'.format(idx+1, score)
# Iterate through 2nd dimension of array (each team member)
for individual_score in score:
team_score = team_score + individual_score
print 'Team {0} score: {1}'.format(idx+1, team_score)
返回:
Team 1 score: 55
Team 2 score: 65
Team 1 score: 55
Team 2 score: 65
Here约为Python列表的详细信息。
在列表上阅读。 2D数组通常使用列表列表在Python中实现。 –