Python: List vs Dict for look up table

后端 未结 8 893
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 10:41

I have about 10million values that I need to put in some type of look up table, so I was wondering which would be more efficient a list or dict?

I know

相关标签:
8条回答
  • 2020-11-22 11:06

    A dict is a hash table, so it is really fast to find the keys. So between dict and list, dict would be faster. But if you don't have a value to associate, it is even better to use a set. It is a hash table, without the "table" part.


    EDIT: for your new question, YES, a set would be better. Just create 2 sets, one for sequences ended in 1 and other for the sequences ended in 89. I have sucessfully solved this problem using sets.

    0 讨论(0)
  • 2020-11-22 11:11

    set() is exactly what you want. O(1) lookups, and smaller than a dict.

    0 讨论(0)
提交回复
热议问题