Data structure for partial multi-keys mapping?

前端 未结 3 1576
小鲜肉
小鲜肉 2021-02-06 08:52

I have data consists of keys mapped to values, like this:

---------------------
Key          | Value
---------------------
(0, 0, 0, 0) | a
(0, 0, 0, 1) | b
(0,          


        
3条回答
  •  没有蜡笔的小新
    2021-02-06 09:17

    Code each relation as a line of text then use a regular expression where '.' can match any single character at that position in the key.

    This would remove any restriction on where the don't cares are.

    Here's some Python:

    >>> import re
    >>> 
    >>> map = '''
    ... 0000 a
    ... 0001 b
    ... 0101 c
    ... 0110 d
    ... '''
    >>> 
    >>> def search(query='0001'):
    ...     matches = re.findall(query + r' .', map)
    ...     return [match.split()[-1] for match in matches]
    ... 
    >>> search('0001')
    ['b']
    >>> search('0...')
    ['a', 'b', 'c', 'd']
    >>> search('01..')
    ['c', 'd']
    >>> 
    

提交回复
热议问题