class Player:
def __init__(self, optional_paramater1, opt_param2):
#Stuff you want to happen when object is initialized
def read_in_matlab_structure(self, matlab_info_as_text):
self.tackles = #Wheverever you get this info from
self.crosses = #...
self.goals = #...
然后,在创建对象时,可以访问自定义名称下面的每个值
players = [] #This would be a list. Depending on your use, you could also
#Use a dictionary that links name to the object
#That way you can call up a person by name.
#eg. players["Babe Ruth"]
#There are also sets in Python that might be applicable to
#Your situation.
for entry in matlab_structure:
temp_player = Player(name)
temp_player.read_in_matlab_structure(entry)
players.append(temp_player)
#then, when you want to access player information,
# lets say you want to see all the players who had more than 8 goals in a season:
for person in players:
if person.goals >= 8:
print(person.name)
就内置数据结构而言,dictionary听起来是最好的选择。 如果需要的话,可以用json module导出它
对于外部数据库,SQLite可能是最佳选择。它需要的设置比其他SQL数据库少得多。如中所示,您只需创建一个.db文件并开始向其中发送数据,而无需任何管理工作
您当然可以使用SQL数据库,但是如果您想严格使用python,您可以利用python的面向对象编程风格。 我将对象类定义为:
然后,在创建对象时,可以访问自定义名称下面的每个值
希望这能给你一个主意。有关Python数据结构的详细信息:
Python 3 Data Structures
相关问题 更多 >
编程相关推荐