简体   繁体   English

如何在Python中动态嵌套现有字典?

[英]How to nest an existing dictionary dynamically in Python?

I have a program that can ask user to categorize and existing dictionary: here is my dictionary: 我有一个程序可以要求用户分类和现有词典:这是我的词典:

>>> print Vocab_World
{'excelent': 'awesome', 'quit': 'exit', 'great': 'good', 'ceken': 
'bilir', 'tremendous': 'fabolous', 'gurbet': 'hasret', 'postpone': 
'put_offt', 'ozlem': 'hasret'}

Now I want to ask the user if they want to categorize the same meaning words as nested dictionary and I want to have a list like below one for the categorized words 现在,我想问用户是否要对与嵌套词典相同的意思词进行分类,并且我想为分类词提供一个像下面这样的列表

{'Adverb' : {'excelent': 'awesome','great': 'good','tremendous': 'fabolous'} }

.

categorized_words = raw_input("Please select the words that you want to categorize")
new_category = raw_input("Please input the new category name ")
categorized_World=Vocab_World[new_category][0][categorized_words]

Is there a way of doing that dynamically based on user input ? 有没有一种方法可以根据用户输入动态地执行此操作?

Vocab_World = {'excelent': 'awesome', 'quit': 'exit', 'great': 'good', 'ceken': 
'bilir', 'tremendous': 'fabolous', 'gurbet': 'hasret', 'postpone': 
'put_offt', 'ozlem': 'hasret'}

Categorized_World = {}  # output dict
chosen_words = []       # array to store all the chosen words

while True:
    categorized_words = raw_input("Words to categorize separated by ',' or Q/q for quit)")
    if categorized_words.lower() == 'q':
        break
    chosen_words.extend(categorized_words.split(','))
    new_category = raw_input("Please input the new category name ")
    Categorized_World[new_category] = {i:Vocab_World[i] for i in
                                       categorized_words.split(',') if i in Vocab_World}

# Add words that are not catgorized
Categorized_World.update({i:Vocab_World[i] for i in Vocab_World if i not in chosen_words})

print(Categorized_World)

Running this and inputing: 运行并输入:

excelent,great,tremendous
adverb
q

Would return: 将返回:

{'adverb': {'excelent': 'awesome', 'great': 'good', 'tremendous': 'fabolous'}, 
'quit': 'exit', 'ceken': 'bilir', 'gurbet': 'hasret', 
'postpone': 'put_offt', 'ozlem': 'hasret'}

This should do it: 应该这样做:

Vocab_World= {'excelent': 'awesome', 'quit': 'exit', 'great': 'good', 'ceken': 
              'bilir', 'tremendous': 'fabolous', 'gurbet': 'hasret', 'postpone': 
              'put_offt', 'ozlem': 'hasret'}

categorized_words = raw_input("Please select the words that you want to categorize ")
new_category = raw_input("Please input the new category name ")

Categorized_World=dict()
tmp=dict()
for w in categorized_words.split():
  tmp.update({w:Vocab_World[w]})

Categorized_World.update({new_category:tmp})
print Categorized_World

Output is: 输出为:

Please select the words that you want to categorize excelent great tremendous
Please input the new category name Adverb
{'Adverb': {'excelent': 'awesome', 'tremendous': 'fabolous', 'great': 'good'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM