无法将两个列表合并到字典中

时间:2017-06-05 17:36:17

标签: python-2.7 list dictionary itertools

我无法将两个列表合并到字典中。我尝试了以下内容:

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

我得到以下内容:

{'word':''} [ '字=']

这两个清单不是空的。

我正在使用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']

2 个答案:

答案 0 :(得分:1)

我认为很多混淆的根源是示例中与不相关的代码。

试试这个:

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

# to remove any extra newline or whitespace from what was read in
map(lambda line: line.rstrip(), lines)
map(lambda word: word.rstrip(), words)

new_dict = dict(zip(words,lines))
print new_dict

Python builtin zip()从每个参数返回一个可迭代的元组。将此可迭代元组赋予dict()对象构造函数会创建一个字典,其中words中的每个项都是键,lines中的项是相应的值。

另请注意,如果words文件的项目数多于lines,则会有任何键为空值。如果lines有项目,则只会使用None键添加最后一项。

答案 1 :(得分:0)

我尝试了这个并为我工作,我创建了两个文件,添加了数字1到4,字母a到d,代码创建了字典确定,我不需要导入itertools,实际上有一个不需要额外的行:

lines = [1,2,3,4]
words = ["a","b","c","d"]


diction = dict(zip(words,lines))
# new_dict = {k: v for k, v in zip(words, lines)}
print(diction)
  

{' a':1,' b':2,' c':3,' d':4}

如果这样做有效,而不是另一个,那么加载列表时一定有问题,请尝试这样加载:

def create_list_from_file(file):
  with open(file, "r") as ins:
    my_list = []
    for line in ins:
      my_list.append(line)
    return my_list

lines = create_list_from_file("/home/vesko_/evnt_classification/bag_of_words")
words = create_list_from_file("/home/vesko_/evnt_classification/sdas")

diction = dict(zip(words,lines))
# new_dict = {k: v for k, v in zip(words, lines)}
print(diction)
  

观察:   如果files.txt看起来像这样:

1
2
3
4

a
b
c
d

结果将包含字典中的键,每行一个:

{'a\n': '1\n', 'b\n': '2\n', 'c\n': '3\n', 'd': '4'}

但是如果你的文件看起来像:

1 2 3 4

a b c d

结果将是{'a b c d': '1 2 3 4'},只有一个值

相关问题