Use String as Variable Name Python

后端 未结 1 1376
不思量自难忘°
不思量自难忘° 2021-01-23 13:48

I am trying to make a database of baseball stats and lineups. Is there a way to loop through a list of teams and use a string as an variable name?

For example: Extract t

相关标签:
1条回答
  • 2021-01-23 14:47

    Use dictionary:

    teams = {}
    
    teams['BOS'] = team_data()   
    teams['NYY'] = team_data() 
    
    for key in teams:
        print(teams[key].team_name) # removed the quotes around "key"
    
    0 讨论(0)
提交回复
热议问题