【发布时间】:2022-11-21 09:28:29
【问题描述】:
我正在尝试制作一个包含字符串和字典的元组列表。字符串是文件名,字典是 n-gram 的频率列表。
('story.txt',
{'back': 12,
'been': 13,
'bees': 58,
'buzz': 13,
'cant': 30,
'come': 12,
'dont': 64,
'down': 16,
'from': 22,
...})
对于我正在做的事情,我想列出这些看起来像的元组
[('story.txt',
{'back': 12,
'been': 13,
'bees': 58,
'buzz': 13,
'cant': 30,
'come': 12,
'dont': 64,
'down': 16,
'from': 22,
...}),
('great_expectations.txt',
{'_he_': 12,
'able': 32,
'aged': 54,
'aint': 56,
'also': 34,
'arms': 44,
'away': 158,
'baby': 23,
...})
]
我正在尝试使用以下代码来做到这一点:
documents = ['story.txt', 'great_expectations.txt']
outputs = []
for document in documents:
doc_map = map_maker.make_map(document, 4, 10)
list_tuple = (document, doc_map)
# pprint.pprint(list_tuple)
outputs.append(list_tuple)
# pprint.pprint(outputs)
出于某种原因,上面的代码在附加字典之前组合了字典中的数据,这样“story.txt”字典将包含最初与“great_expectations.txt”关联的条目,反之亦然,如下所示:
[('story.txt',
{'_he_': 12,
'able': 32,
'aged': 54,
'aint': 56,
'also': 34,
'arms': 44,
'away': 158,
'baby': 23,
'back': 238,
...}),
('great_expectations.txt',
{'_he_': 12,
'able': 32,
'aged': 54,
'aint': 56,
'also': 34,
'arms': 44,
'away': 158,
'baby': 23,
'back': 238,
...})
]
为什么要这样做?我认为元组应该是不可变的。
【问题讨论】:
-
map_maker.make_map()函数在做什么?我会开始在那里搜索错误。
标签: python list dictionary n-gram