热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Ihaveaprogramthatcanaskusertocategorizeandexistingdictionary:hereismydictionary:我有一

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 ?

有没有办法根据用户输入动态执行此操作?

2 个解决方案

#1


0  

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'}

#2


0  

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'}}

推荐阅读
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文介绍了Python对Excel文件的读取方法,包括模块的安装和使用。通过安装xlrd、xlwt、xlutils、pyExcelerator等模块,可以实现对Excel文件的读取和处理。具体的读取方法包括打开excel文件、抓取所有sheet的名称、定位到指定的表单等。本文提供了两种定位表单的方式,并给出了相应的代码示例。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文介绍了蓝桥训练中的闰年判断问题,并提供了使用Python代码进行判断的方法。根据给定的年份,判断是否为闰年的条件是:年份是4的倍数且不是100的倍数,或者是400的倍数。根据输入的年份,输出结果为yes或no。本文提供了相应的Python代码实现。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 31.项目部署
    目录1一些概念1.1项目部署1.2WSGI1.3uWSGI1.4Nginx2安装环境与迁移项目2.1项目内容2.2项目配置2.2.1DEBUG2.2.2STAT ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 本文介绍了在Python中使用FOR循环实现用户输入错误值3次后终止程序的方法。作者提到了自己对这个问题的困惑和尝试,并给出了解决方案。该方案要求代码必须包含FOR循环,但作者不确定是需要一个FOR循环还是3个FOR循环。最后,作者还给出了一些示例代码来说明如何将英里转换为公里和将英寸转换为厘米。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
  • java drools5_Java Drools5.1 规则流基础【示例】(中)
    五、规则文件及规则流EduInfoRule.drl:packagemyrules;importsample.Employ;ruleBachelorruleflow-group ... [详细]
  • 本文总结了使用不同方式生成 Dataframe 的方法,包括通过CSV文件、Excel文件、python dictionary、List of tuples和List of dictionary。同时介绍了一些注意事项,如使用绝对路径引入文件和安装xlrd包来读取Excel文件。 ... [详细]
author-avatar
_妃妃教主
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有