An ordered set of tuples with different hashes and sort keys

I have the following data structure (with sample data):

edgeID (unique key) | timeStep (ordering key,            | value
                    |     can have multiple occurrences) | 
-----------------------------------------------------------------
"edge1"             | 15                                 | 12.1
"edge3"             | 18                                 | 17.32
"edge2"             | 23                                 | 15.1
"edge5"             | 23                                 | 65.6

I want to be able to effectively perform the following tasks in this structure:

  • Add a new data entry with timeStephigher than any other saved timeStep. If maxNumberdata records are reached (e.g. 20), the data record with the smallest timeStepshould be deleted.
  • Combine the two datasets, storing the maxNumberdata records (for example, 20) of the highest timeStemprecords, while retaining each edgeIDno more than once (in case two records for the same edge should use the highest timeStep).

How to implement this data structure in python?

I tried one approach that works:

  • , , SortedSet, :

    data = {}
    dataOrder = SortedSet(key=lambda x: data[x][0])
    maxDataSize = 20
    
    def addData(edgeID, dataTuple):
        if(len(data) >= maxDataSize):
            # remove oldest value
            key = dataOrder.pop(0)
            del data[key]
        # add
        data[edgeID] = dataTuple
        dataOrder.add(edgeID)
    
    addData("edge1", (15, 12.1))
    

    , edgeID .

, :

  1. SortedSet, :

    data = SortedSet(key=lambda x: x[1])
    maxDataSize = 20
    
    def addData(dataTuple):
        if(len(self.data) >= self.maxDataSize):
            # remove oldest value
            data.pop(0)
        # add
        data.add(dataTuple)
    
    addData(("edge1", 15, 12.1))
    

    , , , edgeID timeSteps, ( ) , edgeID. , - OrderedSet. , , , :

  2. , , , __hash__(), edgeID. OrderedSet

? ?

+4
1

, heapq, .

: https://docs.python.org/2/library/heapq.html

, python , O (1). , , , 20 ... >= 20 , heappop ... ...

0

Source: https://habr.com/ru/post/1612740/


All Articles