Python: 添加新的字典键,该键由先前键中的单词组成
Python: Add new dictionary key that consist of word from previous key
所以我使用 python 词典构建了一个词汇表应用程序。但是我发现了一个有趣的案例,我不知道如何解决它。
比如我有一个字典如下
glos = {'Hypertext Markup Language': 'the standard markup language for documents.',
'HTML': 'the standard markup language for documents.',
'Semantic Hypertext Markup Language': 'HTML that emphasizes the meaning of the encoded information.'}
如您所见,Hypertext Markup Language
与 HTML
具有相同的值,但是有什么方法可以添加与 Semantic Hypertext Markup Language
具有相同值的 Semantic HTML
键?
最终产品是这样的:
glos = {'Hypertext Markup Language': 'the standard markup language for documents.',
'HTML': 'the standard markup language for documents.',
'Semantic Hypertext Markup Language': 'HTML that emphasizes the meaning of the encoded information.'
'Semantic HTML': 'HTML that emphasizes the meaning of the encoded information.'}
我想用这样的缩写为键创建新的字典
same_val = {'Hypertext Markup Language': 'HTML'}
之后,它会循环 glos
dict 中的键,通过使用一些正则表达式或任何东西来查找它是否有来自 same_val
dict 的单词,但我不知道如何正确在代码中键入它。
您想将新键 'Semantic HTML'
插入到 glos
中,其值与键 'Semantic Hypertext Markup Language'
的值相同。如果我的理解是正确的,应该完成您的需求的代码如下。在创建 glos
对象后添加此内容。
glos['Semantic HTML'] = glos['Semantic Hypertext Markup Language']
这是做什么的?我们从字典中获取对应于键 'Semantic Hypertext Markup Language'
的值,并将相同的值分配回字典,在键 'Semantic HTML'
.
下
这听起来像是一个非常脆弱的方法。它不适用于复数等变体。我建议不要膨胀主词汇表,而是使用单独的 aliases
词典,并在查找术语时一起使用 glos
和 aliases
。例如:
glos = {'Hypertext Markup Language': 'the standard markup language for documents.',
'Semantic Hypertext Markup Language': 'HTML that emphasizes the meaning of the encoded information.'}
aliases = {'HTML': 'Hypertext Markup Language',
'Semantic HTML': 'Semantic Hypertext Markup Language'}
def lookup(term):
return glos.get(term, glos.get(aliases.get(term)))
所以我使用 python 词典构建了一个词汇表应用程序。但是我发现了一个有趣的案例,我不知道如何解决它。
比如我有一个字典如下
glos = {'Hypertext Markup Language': 'the standard markup language for documents.',
'HTML': 'the standard markup language for documents.',
'Semantic Hypertext Markup Language': 'HTML that emphasizes the meaning of the encoded information.'}
如您所见,Hypertext Markup Language
与 HTML
具有相同的值,但是有什么方法可以添加与 Semantic Hypertext Markup Language
具有相同值的 Semantic HTML
键?
最终产品是这样的:
glos = {'Hypertext Markup Language': 'the standard markup language for documents.',
'HTML': 'the standard markup language for documents.',
'Semantic Hypertext Markup Language': 'HTML that emphasizes the meaning of the encoded information.'
'Semantic HTML': 'HTML that emphasizes the meaning of the encoded information.'}
我想用这样的缩写为键创建新的字典
same_val = {'Hypertext Markup Language': 'HTML'}
之后,它会循环 glos
dict 中的键,通过使用一些正则表达式或任何东西来查找它是否有来自 same_val
dict 的单词,但我不知道如何正确在代码中键入它。
您想将新键 'Semantic HTML'
插入到 glos
中,其值与键 'Semantic Hypertext Markup Language'
的值相同。如果我的理解是正确的,应该完成您的需求的代码如下。在创建 glos
对象后添加此内容。
glos['Semantic HTML'] = glos['Semantic Hypertext Markup Language']
这是做什么的?我们从字典中获取对应于键 'Semantic Hypertext Markup Language'
的值,并将相同的值分配回字典,在键 'Semantic HTML'
.
这听起来像是一个非常脆弱的方法。它不适用于复数等变体。我建议不要膨胀主词汇表,而是使用单独的 aliases
词典,并在查找术语时一起使用 glos
和 aliases
。例如:
glos = {'Hypertext Markup Language': 'the standard markup language for documents.',
'Semantic Hypertext Markup Language': 'HTML that emphasizes the meaning of the encoded information.'}
aliases = {'HTML': 'Hypertext Markup Language',
'Semantic HTML': 'Semantic Hypertext Markup Language'}
def lookup(term):
return glos.get(term, glos.get(aliases.get(term)))