def unpack_dict(matrix, map_index_to_word):
    table = sorted(map_index_to_word, key=map_index_to_word.get)
    data = matrix.data
    indices = matrix.indices
    indptr = matrix.indptr
    num_doc = matrix.shape[0]
    return [{k:v for k,v in zip([table[word_id] for word_id in
    indices[indptr[i]:indptr[i+1]] ],
    data[indptr[i]:indptr[i+1]].tolist())} \
               for i in range(num_doc) ]

wiki['tf_idf'] = unpack_dict(tf_idf, map_index_to_word)


python - 谁能解释这个列表理解?-LMLPHP

map_index_to_word是word:index的字典,包含数千个单词。
tf_idf是TFIDF稀疏向量
DataFrame Wiki在此处的屏幕快照中显示

最佳答案

[{k: v for k, v in zip([table[word_id] for word_id in indices[indptr[i]:indptr[i + 1]]],data[indptr[i]:indptr[i + 1]].tolist())} for i in range(num_doc)]


与:

final_list = []
for i in range(num_doc):
    new_list = []
    for word_id in indices[indptr[i]:indptr[i + 1]]:
        new_list.append(table[word_id])

    new_dict = {}
    for k, v in zip(new_list, data[indptr[i]:indptr[i + 1]].tolist()):
        new_dict[k] = v
    final_list.append(new_dict)

关于python - 谁能解释这个列表理解?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46757220/

10-12 23:06