Python: Tuples/dictionaries as keys, select, sort

后端 未结 8 1707
独厮守ぢ
独厮守ぢ 2020-12-07 07:53

Suppose I have quantities of fruits of different colors, e.g., 24 blue bananas, 12 green apples, 0 blue strawberries and so on. I\'d like to organize them in a data structur

8条回答
  •  执笔经年
    2020-12-07 08:46

    This type of data is efficiently pulled from a Trie-like data structure. It also allows for fast sorting. The memory efficiency might not be that great though.

    A traditional trie stores each letter of a word as a node in the tree. But in your case your "alphabet" is different. You are storing strings instead of characters.

    it might look something like this:

    root:                Root
                         /|\
                        / | \
                       /  |  \     
    fruit:       Banana Apple Strawberry
                  / |      |     \
                 /  |      |      \
    color:     Blue Yellow Green  Blue
                /   |       |       \
               /    |       |        \
    end:      24   100      12        0
    

    see this link: trie in python

提交回复
热议问题