Python Dictionary仅存储键值

时间:2015-07-13 02:00:47

标签: python dictionary output

伙计们我有这个代码:

text =  []
pos  =  {}
neg  =  {}
h = HTMLParser.HTMLParser()
i = 0

with open('DATA/test.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        i += 1
        token = word_tokenize(h.unescape(row[3].decode('utf-8')))
        # print(h.unescape(row[1] + ' ' + row[3].decode('utf8')))
        if row[1] == "0":
            pos[i] = {i: token}
        elif row[1] == "1":
            neg.update({i: token})

        text += token

text = nltk.Text(text)
neg  = nltk.Text(neg)
pos  = nltk.Text(pos)

print(pos)

我希望每个neg或pos的格式为{1:'whatever'},{2:'another'}但是我只得到关键值,如:Text:1 2 3 4 9 10 20 24 ...

1 个答案:

答案 0 :(得分:1)

nltk.Text的构造函数需要str的可迭代。当dict用作可迭代时,只迭代密钥。

您可以通过调用(key, value)上的.items()来获得dict对的可迭代次数,但这不会满足nltk.Text构造函数。

相关问题