将list()函数应用于字典时会发生什么?

时间:2018-05-12 05:33:05

标签: python-3.x

我在理解python中toarray()和list()函数之间的差异时遇到了一些麻烦。请考虑以下代码:

from gensim.models import Word2Vec

sentences_word2vec = [['this', 'is', 'the', 'first', 'test', 'sentence'],
            [ 'this', 'is', 'the', 'second', 'one', 'in', 'the', 'test'],
            ['we', 'need', 'a', 'second-last', 'test', 'sentence', 'for', 'our', 'test','script'], 
            ['this', 'is', 'the', 'last', 'one', 'now', 'we"re', 'done']]

model = Word2Vec(sentences_word2vec, min_count=1)
print(list(model.wv.vocab))

如果我运行此脚本,我会得到以下输出:

['this', 'is', 'the', 'first', 'test', 'sentence', 'second', 'one', 'in', 'we', 
'need', 'a', 'second-last', 'for', 'our', 'script', 'last', 'now', 'we are', 'done']

这就是我想要的,但在这种情况下,我并没有真正理解list()函数的工作原理。如果我在没有它的情况下运行脚本,那就是运行print(model.wv.vocab),我得到以下输出:

{'this': <gensim.models.keyedvectors.Vocab object at 0x1072c2828>, 
'is': <gensim.models.keyedvectors.Vocab object at 0x1072c2860>, 
'the': <gensim.models.keyedvectors.Vocab object at 0x1072c2898>, 
'first': <gensim.models.keyedvectors.Vocab object at 0x1072c28d0>, 
'test': <gensim.models.keyedvectors.Vocab object at 0x1072c2908>, 
'sentence': <gensim.models.keyedvectors.Vocab object at 0x1072c2940>, 
'second': <gensim.models.keyedvectors.Vocab object at 0x1072c2978>, 
'one': <gensim.models.keyedvectors.Vocab object at 0x1072c29b0>,
'in': <gensim.models.keyedvectors.Vocab object at 0x1072c29e8>, 
'we': <gensim.models.keyedvectors.Vocab object at 0x1072c2a20>, 
'need': <gensim.models.keyedvectors.Vocab object at 0x1072c2a58>, 
'a': <gensim.models.keyedvectors.Vocab object at 0x1072c2a90>, 
'second-last': <gensim.models.keyedvectors.Vocab object at 0x1072c2ac8>, 
'for': <gensim.models.keyedvectors.Vocab object at 0x1072c2b00>, 
'our': <gensim.models.keyedvectors.Vocab object at 0x1072c2b38>, 
'script': <gensim.models.keyedvectors.Vocab object at 0x1072c2b70>, 
'last': <gensim.models.keyedvectors.Vocab object at 0x1072c2ba8>, 
'now': <gensim.models.keyedvectors.Vocab object at 0x1072c2be0>, 
'we are': <gensim.models.keyedvectors.Vocab object at 0x1072c2c18>, 
'done': <gensim.models.keyedvectors.Vocab object at 0x1072c2c50>}

问题list()如何将这个神秘的词典(至少对我来说非常神秘)变成了很好的词汇?

1 个答案:

答案 0 :(得分:2)

list(d)会返回d的键列表(也可以d.keys()访问)。如果将字典视为可迭代的(例如,也在for循环中),则会生成其密钥。