我不能两个列表合并成一个dictionary.I尝试了以下内容:不能合并两个列表成为一个字典
Map two lists into a dictionary in Python
我尝试了所有的解决方案,我仍然得到一个空的字典
from sklearn.feature_extraction import DictVectorizer
from itertools import izip
import itertools
text_file = open("/home/vesko_/evnt_classification/bag_of_words", "r")
text_fiel2 = open("/home/vesko_/evnt_classification/sdas", "r")
lines = text_file.read().split('\n')
words = text_fiel2.read().split('\n')
diction = dict(itertools.izip(words,lines))
new_dict = {k: v for k, v in zip(words, lines)}
print new_dict
我得到以下:
{ '单词': ''} [ '字=']
这两个列表不是空的。
我使用python2.7
编辑:
从两个列表输出(我只显示几个,因为它是用11K特征向量)
//lines
['change', 'I/O', 'fcnet2', 'ifconfig',....
//words
['word', 'word', 'word', .....
编辑:
现在至少我有一些输出@DamianLattenero
{'word\n': 'XXAMSDB35:XXAMSDB35_NGCEAC_DAT_L_Drivei\n'}
['word\n=XXAMSDB35:XXAMSDB35_NGCEAC_DAT_L_Drivei\n']
打印出'lines'和'words'是为了确保工作正常 – MrJLP
您还需要额外的导入。在这个例子中'DictVectorizer'没有使用,可能'itertools'不需要,如下面的回答所示 – MrJLP
@MrJLP这是正确的,问题应该在数据加载中 –