Is there a Python data structure that can be sortable and searchable?

I am using python to control the line queue for processing. He has several requirements:

  • Each line corresponds to priority and is processed only on the basis of this value.
  • Lines can be added to this queue dynamically, but duplicate lines are not allowed in the queue. If a duplicate is presented, it must be identified and ignored.

So, is there any python data type that will allow something like this? Or do I need to write my own?

If there is no native, I think about maintaining two structures.

  • A heapq that will support strings and their priority
  • A list that supports a hash of strings to check if a string is already saved

Until they fall out of sync, he should solve the problem.

+3
source share
2 answers

That sounds like a reasonable approach. I would use setinstead listbecause it has a more efficient membership check and you don't need to maintain order (since you do this in heapq)

+5
source

An ordered dictionary may be useful.

See this page ( ordered dictionary ) for:

The ordered dictionary holds the keys in input order. This is sometimes called a created order dictionary.

, , .

setkeys, , . , , .

+2

All Articles