我想编一本节本词典。你知道吗
我有一个有很多删节的文本文件。文本文件如下所示(导入后)
with open('abreviations.txt') as ab:
ab_words = ab.read().splitlines()
摘录:
'ACE',
'Access Control Entry',
'ACK',
'Acknowledgement',
'ACORN',
'A Completely Obsessive Really Nutty person',
现在我要构建dictionary,其中每一个不均匀的行作为字典键,每一个偶数行作为字典值。你知道吗
因此,我应该能够在结尾写道:
ab_dict['ACE']
得到结果:
'Access Control Entry'
另外,如何使其不区分大小写?你知道吗
ab_dict['ace']
应该得到同样的结果
'Access Control Entry'
事实上,如果输出也是小写的话,那就太完美了:
'access control entry'
下面是指向文本文件的链接:https://www.dropbox.com/s/91afgnupk686p9y/abreviations.txt?dl=0
具有自定义
ABDict
类和Python生成器功能的完整解决方案:现在,测试(使用case relative访问):
输出(连续):
以下是一个答案,也允许用字典中的单词替换句子:
输出:
下面是另一个基于this solution函数的解决方案:
相关问题 更多 >
编程相关推荐