Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-dimensional list in Python?

Tags:

python

list

So I have this code:

Team1 = ["Red", 10]
Team2 = ["Green", 5]
Team3 = ["Blue", 6]
Team4 = ["Yellow", 8]
Team5 = ["Purple", 9]
Team6 = ["Brown", 4]

TeamList = [Team1, Team2, Team3, Team4, Team5, Team6]

I want to make a two-dimensional list of difference in scores between each pair of teams. The output can be like this: Output

What is the easiest way to do this? Thanks :)

like image 808
DarsAE Avatar asked May 16 '26 05:05

DarsAE


2 Answers

You could try:

[[x[1]-y[1] for y in TeamList] for x in TeamList]

That will generate a nested list representing the proposed output (without the column and row headings, of course).

like image 80
phimuemue Avatar answered May 18 '26 17:05

phimuemue


Just using tabs rather than any fancy formatting to build the chart:

Team1 = ["Red", 10]
Team2 = ["Green", 5]
Team3 = ["Blue", 6]
Team4 = ["Yellow", 8]
Team5 = ["Purple", 9]
Team6 = ["Brown", 4]

TeamList = [Team1, Team2, Team3, Team4, Team5, Team6]

# print the top row of team names, tab separated, starting two tabs over:
print '\t\t', '\t'.join(team[0] for team in TeamList)

# for each row in the chart
for team in TeamList:
    # put two tabs between each score difference column
    scoreline = '\t\t'.join(str(team[1] - other[1]) for other in TeamList)
    # and print the team name, a tab, then the score columns
    print team[0], '\t', scoreline
like image 38
agf Avatar answered May 18 '26 18:05

agf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!