2017-05-26 99 views
1

所以我在MATLAB中有一個龐大的結構,其中包含英國首屈一指的每個玩家(其中688個)。在單個球員的區域內,有更多陣列包含本賽季每場比賽的各種統計數據(剷球,十字架,球門等)。要使用哪種python數據結構

所以爲了澄清,我有一個688長度的結構,包含大約40個元素。每個元素是一個包含38個條目的數組。

你會用什麼數據結構來存儲這些數據?我一直在閱讀關於關係數據庫,並認爲我應該開始學習PostgreSQL ..我只想問在這裏你會用什麼?

回答

1

你絕對可以使用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

+0

太神奇了!謝謝,這是完美的 –

1

就內置的數據結構而言,dictionary聽起來像是您最好的選擇。 如果有必要,您可以用json module將其導出。

對於外部數據庫,SQLite可能是您的最佳選擇。它需要比其他SQL數據庫少很多的設置。正如你可以創建一個.db文件並開始發送數據給它,而不需要任何管理工作。