简体   繁体   English

如何使用字符串搜索变量? / 你能在 class 中搜索变量吗?

[英]how to search for a variable using a string? / can you search a class for a variable?

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.我正在从文件中获取团队的名称,我需要一种方法来找到同名的变量,所有团队名称变量都在名为 team 的 class 下,有没有办法搜索变量。 I don't know if it would be easier to do with a dictionary or not.我不知道用字典是否会更容易。 Python 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.使用 for 循环是因为我们将查看结果列表并将该数据用于某些用途。 Thanks谢谢

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

# sample.py
from pprint import pprint

variable = 'some value'
x = 20

pprint(globals())

Output: 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.正如你所看到的,所有定义的变量都将被添加到globals()并且它是一个dict ,这意味着你可以从中获取值。

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: 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 名称应以大写字母开头。 class Team: instead of class team: class Team:代替class team:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM