对我们来说是哪种python数据结构

2024-09-27 21:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在MATLAB中有一个巨大的结构,包含了英超的每一个球员(688人)。在一个球员的区域内,有更多的数组,包含了本赛季每场比赛的各种统计数据(铲球、传中、进球等)

为了澄清,我有一个688长度的结构,包含大约40个元素。每个元素是一个包含38个条目的数组

您将使用什么数据结构来存储这些数据?我一直在读关于关系数据库的书,我想我应该开始学习PostgreSQL。。我只是想问问你用什么


Tags: 数据目的区域元素数据结构postgresql数组结构
2条回答

就内置数据结构而言,dictionary听起来是最好的选择。 如果需要的话,可以用json module导出它

对于外部数据库,SQLite可能是最佳选择。它需要的设置比其他SQL数据库少得多。如中所示,您只需创建一个.db文件并开始向其中发送数据,而无需任何管理工作

您当然可以使用SQL数据库,但是如果您想严格使用python,您可以利用python的面向对象编程风格。 我将对象类定义为:

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)

希望这能给你一个主意。有关Python数据结构的详细信息:

Python 3 Data Structures

相关问题 更多 >

    热门问题