So I build a glossary application using python dictionary. But I found an interesting case which I don't know how to solve it.
For example, I have a dictionary as follows.
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.'}
As you can see, the Hypertext Markup Language
has the same value as HTML
, but is there any way to add Semantic HTML
key with the same value as Semantic Hypertext Markup Language
?
The end product would be like this:
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.'}
I thought of creating new dict for key with its abbreviation like this
same_val = {'Hypertext Markup Language': 'HTML'}
After that, it's gonna loop the key in glos
dict to find if it has the word from same_val
dict by using some regex or anything, but I don't know how to properly type it in code.
This sounds like a very fragile method. It won't work with variations such as plurals etc. I would suggest against bloating the main glossary but having a separate aliases
dictionary and use glos
and aliases
together when looking up terms. For example:
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)))
You want to insert a new key 'Semantic HTML'
into glos
with the same value as for the key 'Semantic Hypertext Markup Language'
. If my understanding is correct, the code that should accomplish your needs is as follows. Add this after you've created the glos
object.
glos['Semantic HTML'] = glos['Semantic Hypertext Markup Language']
What does this do? We get the value corresponding to the key 'Semantic Hypertext Markup Language'
from the dictionary, and assign that same value back to the dictionary, under the key 'Semantic HTML'
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.