简体   繁体   English

命名2D数组的最佳Python数据结构是什么

[英]What is the best Python data structure for named 2D array

I am new to Python. 我是Python的新手。 I have two dimensional data. 我有二维数据。 Lets say "Brand" and "Respondent". 让我们说“品牌”和“受访者”。 I want to perform some calculations on this data. 我想对该数据进行一些计算。 For each brand and respondent there is a corresponding rating. 对于每个品牌和受访者,都有一个相应的评分。 ie: 即:

Pepsi -> Respondent 1 -> 8 Coke -> Respondent 3 -> 10 百事可乐->受访者1-> 8可乐->受访者3-> 10

I could store this in a 2D list, but I like to retain the references for the brands and respondent number/name somehow. 我可以将其存储在2D列表中,但我希望保留品牌的参考文献以及被访者的电话号码/名称。

What is the best data structure for this? 最佳的数据结构是什么? Perhaps I could look them up as: 也许我可以将它们查找为:

random_data["Pepsi"][123]

or iterate through each respondent and do some math on each brand. 或遍历每个受访者,并对每个品牌进行一些数学计算。

It depends on what you need to do with the data, dictionaries are a good start: 这取决于您需要处理的数据,字典是一个很好的开始:

mydict = {"Pepsi": {"respondent": 3, "rating": 8}, "Coke": {"respondent": 3, "rating":10}}

You can access it using: 您可以使用以下方式访问它:

mydict["Pepsi"]["rating"]

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

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