python的笔记

  np.argsort(x):返回数据x从小到大的索引值,记住是一个索引值。当你想获取从小到大的数据的时候,用到这个

  data[::-1]:对数据data进行倒转

1 a=[0,1,2,3,4,5]
2 a[::-1]
3 Out[3]: [5, 4, 3, 2, 1, 0]
4 import numpy as np
5 np.argsort(a)
6 Out[5]: array([0, 1, 2, 3, 4, 5], dtype=int64)

   list是通过下标访问的,我们可以使用已经获得的下标获取list里面的数据

1 topic_to_topwords = {}
2 for j, topic_to_word in enumerate(dat['topic_term_dists']):
3     top = np.argsort(topic_to_word)[::-1][:top_n]               # 概率从大到小的下标索引值
4     msg = 'Topic %i '  % j
5     oo = dat['vocab'][254].strip()
6     tt = dat['vocab'][254].strip()[:35]
7     top_words = [dat['vocab'][i].strip()[:35] for i in top]
8     msg += ' '.join(top_words)
 1 for j, topic_to_word in enumerate(dat['topic_term_dists']):
 2     top = np.argsort(topic_to_word)[::-1][:top_n]               # 概率从大到小的下标索引值
 3     msg = 'Topic %i '  % j
 4     # 通过list的下标获取关键词
 5     top_words = [dat['vocab'][i].strip()[:35] for i in top]
 6     # 数据拼接
 7     msg += ' '.join(top_words)
 8     print(msg)
 9     # 将数据保存到字典里面
10     topic_to_topwords[j] = top_words

  快速的将list保存到字典数据中

原文地址:https://www.cnblogs.com/demo-deng/p/9706611.html