Data structure for maintaining tabular data in memory?

前端 未结 6 1994
孤城傲影
孤城傲影 2020-12-04 05:41

My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent,

6条回答
  •  攒了一身酷
    2020-12-04 06:24

    I personally would use the list of row lists. Because the data for each row is always in the same order, you can easily sort by any of the columns by simply accessing that element in each of the lists. You can also easily count based on a particular column in each list, and make searches as well. It's basically as close as it gets to a 2-d array.

    Really the only disadvantage here is that you have to know in what order the data is in, and if you change that ordering, you'll have to change your search/sorting routines to match.

    Another thing you can do is have a list of dictionaries.

    rows = []
    rows.append({"ID":"1", "name":"Cat", "year":"1998", "priority":"1"})
    

    This would avoid needing to know the order of the parameters, so you can look through each "year" field in the list.

提交回复
热议问题