1

I am taking the name of a team from a file and I need a way to find a variable of the same name, all the team name variables are under a class called team, is there a way to search for the variable. I don't know if it would be easier to do with a dictionary or not. Python

class team()...

Burnley = team("Burnley",[1, 0, 0, 1], True)
Southampton = team("Southampton", [1,2,1,0,0,0,1], True)
Swansea = team("Swansea",[1,2,1,1,1,2,1,1,0,0,0,0,0,0,3],True)

df = pd.read_csv("2016-17 testing.csv")
df =df[["Date","HomeTeam","AwayTeam","FTHG","FTAG","FTR"]]

for game in df.iterrows():

    home_team = df["HomeTeam"]
    away_team = df["AwayTeam"]

It is here where I need to search for the home team and find the variable. The for loop is used because we'll be going down a list of results and putting that data to some use. Thanks

  • Using a dictionary will help you solve the problem. Another possible way is just to make a list of all the teams. Then, you can search in this list for a team with a name equal to whatever needed. – Moosa Saadat Jun 21 '20 at 11:58
  • Also, it is a good practice to start your class name with a capital letter. So, it should be named `Team`. – Moosa Saadat Jun 21 '20 at 11:59
  • @MoosaSaadat The list sounds like a good idea, how would I search the list for the home team name? Can you make lists of variables?? And I will change that thanks – Prince_persia22 Jun 21 '20 at 12:06
  • I am assuming your `team` class has a `name` attribute which stores the name of the team: `team("Burnley",[1, 0, 0, 1], True)`. Like here, you are passing `"Burnley"` which is the team name. So, once, you have created a list of teams, you can check if the `team.name` matches the name. For example, `teams = [team("Burnley",[1, 0, 0, 1], True), team("Southampton", [1,2,1,0,0,0,1], True)]` `for t in teams:` `if t.name == "Burnley":` – Moosa Saadat Jun 21 '20 at 12:13

1 Answers1

0

The best way would be to use dictionaries.

teams = {
    'Burnley': team("Burnley",[1, 0, 0, 1], True),
    'Southampton': team("Southampton", [1,2,1,0,0,0,1], True),
    'Swansea': team("Swansea",[1,2,1,1,1,2,1,1,0,0,0,0,0,0,3], True)
}

# and then simply fetch values from it
Burnley = teams['Burnley']

...
for idx, game in df.iterrows():
    home_team_name = game["HomeTeam"]  # Burnley let's assume
    home_team = teams[home_team_name] 
    ...

BUT, if you really want what the question asks for, keep reading.

Let's say I have a file, sample.py

# sample.py
from pprint import pprint

variable = 'some value'
x = 20

pprint(globals())

Output:

$ python3 sample.py 
{'__annotations__': {},
 '__builtins__': <module 'builtins' (built-in)>,
 '__cached__': None,
 '__doc__': None,
 '__file__': 'sample.py',
 '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fccad01eb80>,
 '__name__': '__main__',
 '__package__': None,
 '__spec__': None,
 'pprint': <function pprint at 0x7fccacefdf70>,
 'variable': 'some value',
 'x': 20}

As you can see, all variables defined will be added to globals() and it's a dict, which means you can get the values from it.

Therefore you can do something like this

# sample.py
variable = 'some value'
x = 20

variable_name = 'variable'
number_name = 'x'

value_of_variable = globals().get(variable_name)
print(f'Value of variable = {value_of_variable}')

value_of_x = globals().get(number_name)
print(f'Value of x = {value_of_x}')

Output:

Value of variable = some value
Value of x = 20

So for your case,

...

for idx, game in df.iterrows():
    home_team_name = game["HomeTeam"]  # Burnley let's assume
    home_team = globals().get(home_team_name)  # would give you the value of the variable `Burnley` if it exists
    ...

If you've read the entire answer up to here, as a suggestion I'd like to add that class names should begin with a capital letter. class Team: instead of class team:

Diptangsu Goswami
  • 5,554
  • 3
  • 25
  • 36