Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python数据结构,采用<;非子字符串键,值>;独一无二_Python_Python 3.x_Algorithm_Class_Dictionary - Fatal编程技术网

Python数据结构,采用<;非子字符串键,值>;独一无二

Python数据结构,采用<;非子字符串键,值>;独一无二,python,python-3.x,algorithm,class,dictionary,Python,Python 3.x,Algorithm,Class,Dictionary,Python字典将键视为唯一的。但我正在努力编写(找到)一个支持以下内容的DS: 示例1: |------|-----------------| | abc | matched in abcD | |------|-----------------| | match| matched in abcD | |------|-----------------| | abc | abc found again | 如果下一个条目是: |-----|-----------------|

Python字典将
视为唯一的。但我正在努力编写(找到)一个支持以下内容的DS:

示例1

|------|-----------------|
| abc  | matched in abcD |
|------|-----------------|
| match| matched in abcD |
|------|-----------------|
| abc  | abc found again |
如果下一个条目是:

|-----|-----------------|                     
| abcd| matched in abcD |  ----> should replace first entry because abc is substring of abcd.
因此,条目/表应该如下所示

|------|-----------------|
| abcd | matched in abcD |
|------|-----------------|
| match| matched in abcD |
|------|-----------------|
| abc  | abc found again | 
有什么建议吗?

可能的函数定义

def substring_value_confirm(keys, values):
    # implement as above
    return required_mapping

substring_value_confirm(["abc","match","abc","abcd"],["matched in abcD", "matched in abcD", "abc found again", "matched in abcD"])
对于任何新条目,首先应匹配
,如果
存在,则将检查
,如果一个
是另一个的子字符串,则应选择较长的一个

示例2:

|------|----|
| a    | 1  |
|------|----|
| b    | 2  |
|------|----|
| ba   | 3  |
如果新条目

|------|----|
| ab   | 1  |   ----> should replace first
|------|----|
| b    | 2  |   ----> should be discarded because already present
|------|----|
| ab   | 3  |   ----> will be new insertion
因此,结果条目/表格应为:

|------|----|
| ab   | 1  |
|------|----|
| b    | 2  |
|------|----|
| ba   | 3  |
|------|----|
| ab   | 3  |

对数据结构有算法要求吗?我猜
O(1)
“访问”和“插入”可能是不可能的。
O(n)
足够吗?最理想的总是好的,但到目前为止,我有兴趣知道方法/解决方案。关于第二个
abc
,你所说的
abc再次发现是什么意思?这是一个值吗?另外,任何字符串都是其自身的子字符串,这与第二个条件相矛盾。你的意思是,如果两个键都相同,你应该将其添加到字典中?对于任何新条目,首先应该匹配值,如果值存在,则
key
检查将开始,如果
key
满足子字符串标准,则应该获得更长的位置。我认为前缀树以及表示条目的元组列表将工作前缀树用于检查此键的子字符串是否在线性时间内存在(键的长度为线性)。前缀树的每个节点都可以包含一个索引列表,这些索引对应于从根到该节点的路径作为键的条目。明天早上我会写一份答复。