2015-11-05 72 views
-1

我想弄清楚2D數組是如何工作的,如果有人可以向我解釋如何使用二維數組編碼一個表,其中有一個列中有團隊1和2玩家在第二欄中有一個積分值。一旦我完成了這些,我需要能夠將球隊1的成績加起來,並且讓球隊2的成績上升。共有20名球員。Python 2D陣列團隊表

謝謝!

+0

在列表上閱讀。 2D數組通常使用列表列表在Python中實現。 –

回答

1

看看下面的例子:

# 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列表的詳細信息。