0

Let's say I have a list of soccer players. For now, I only have four players. [Messi, Iniesta, Xavi, Neymar]. More players will be added later on. I want to keep track of the number of times these soccer players pass to each other during the course of a game. To keep track of the passes, I believe I'll need a data structure similar to this

Messi = {Iniesta: 4, Xavi: 5 , Neymar: 8}
Iniesta = {Messi: 4, Xavi: 10 , Neymar: 5}
Xavi  = {Messi: 5, Iniesta: 10 , Neymar: 6}
Neymar = {Messi: 8, Iniesta: 5 , Xavi: 6}

Am I right to use a dictionary? If not, what data structure would be better suited? If yes, how do I approach this using a dictionary though? How do I address the issue of new players being included from time to time, and creating a dictionary for them as well. As an example, If I get the first element in the list, List(i) in the first iteration is Messi, how do i use the value stored in it to create a dictionary with the name Messi. That is how do i get the line below.

Messi = [Iniesta: 4, Xavi: 5 , Neymar: 8]

It was suggested I try something like this

my_dynamic_vars = dict()
string = 'someString'

my_dynamic_vars.update({string: dict()})

Python and programming newbie here. Learning with experience as I go along. Thanks in advance for any help.

2
  • You can use defaultdict from collections module. It is the easiest way to handle this task. Here is the link to documentation and examples. Commented Feb 23, 2014 at 1:49
  • This is a side note- but if you want to make a dictionary, you want to use {} instead of [] (makes a list). So Messi = [Iniesta: 4, Xavi: 5 , Neymar: 8] would be Messi = {Iniesta: 4, Xavi: 5 , Neymar: 8}, given that Neymar etc. are objects. Commented Feb 23, 2014 at 2:59

2 Answers 2

2

This is a fun question, and perhaps a good situation where something like a graph might be useful. You could implement a graph in python by simply using a dictionary whose keys are the names of the players and whose values are lists players that have been passed the ball.

passes = {
    'Messi' : ['Iniesta', 'Xavi','Neymar', 'Xavi', 'Xavi'],
    'Iniesta' : ['Messi','Xavi', 'Neymar','Messi', 'Xavi'],
    'Xavi'  : ['Messi','Neymar','Messi','Neymar'],
    'Neymar' : ['Iniesta', 'Xavi','Iniesta', 'Xavi'],
}

To get the number of passes by any one player:

len(passes['Messi'])

To add a new pass to a particular player:

passes['Messi'].append('Xavi')

To count the number of times Messi passed to Xavi

passes['Messi'].count('Xavi')

To add a new player, just add him the first time he makes a pass

passes['Pele'] = ['Messi']

Now, he's also ready to have more passes 'appended' to him

passes['Pele'].append['Xavi']

What's great about this graph-like data structure is that not only do you have the number of passes preserved, but you also have information about each pass preserved (from Messi to Iniesta)

And here is a super bare-bones implementation of some functions which capture this behavior (I think a beginner should be able to grasp this stuff, let me know if anything below is a bit too confusing)

passes = {}

def new_pass(player1, player2):
    # if p1 has no passes, create a new entry in the dict, else append to existing
    if player1 not in passes:
        passes[player1] = [player2]
    else:
        passes[player1].append(player2)

def total_passes(player1):
    # if p1 has any passes, return the total number; otherewise return 0
    total = len(passes[player1]) if player1 in passes else 0
    return total

def total_passes_from_p1_to_p2(player1, player2):
    # if p1 has any passes, count number of passes to player 2; otherwise return 0
    total = passes[player1].count(player2) if player1 in passes else 0
    return total

Ideally, you would be saving passes in some database that you could continuously update, but even without a database, you can add the following code and run it to get the idea:

# add some new passes!
new_pass('Messi', 'Xavi')
new_pass('Xavi', 'Iniesta')
new_pass('Iniesta', 'Messi')
new_pass('Messi', 'Iniesta')
new_pass('Iniesta', 'Messi')

# let's see where we currently stand
print total_passes('Messi')
print total_passes('Iniesta')
print total_passes_from_p1_to_p2('Messi', 'Xavi')

Hopefully you find this helpful; here's some more on python implementation of graphs from the python docs (this was a fun answer to write up, thanks!)

Sign up to request clarification or add additional context in comments.

Comments

1

I suggest you construct a two dimensional square array. The array should have dimensions N x N. Each index represents a player. So the value at passes[i][j] is the number of times the player i passed to player j. The value passes[i][i] is always zero because a player can't pass to themselves

Here is an example.

players = ['Charles','Meow','Rebecca']

players = dict( zip(players,range(len(players)) ) )
rplayers = dict(zip(range(len(players)),players.keys()))

passes = []
for i in range(len(players)):
    passes.append([ 0 for i in range(len(players))])

def pass_to(f,t):
    passes[players[f]][players[t]] += 1

pass_to('Charles','Rebecca')
pass_to('Rebecca','Meow')
pass_to('Charles','Rebecca')

def showPasses():
    for i in range(len(players)):
        for j in range(len(players)):
            print("%s passed to %s %d times" % ( rplayers[i],rplayers[j],passes[i][j],))

showPasses()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.