[根据词频排序]使用operator.itemgetter(1)

2019-01-08  本文已影响8人  VanJordan
return sorted(
      vocab_freq_map.items(), key=operator.itemgetter(1), reverse=True)
a = [1,2,3] 
>>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值
>>> b(a) 
2 
>>> b=operator.itemgetter(1,0)   //定义函数b,获取对象的第1个域和第0个的值
>>> b(a) 
(2, 1) 
  vocab_freqs = dict((term, freq) for term, freq in vocab_freqs.items()
                     if doc_counts[term] > FLAGS.doc_count_threshold)

  # Sort by frequency
  ordered_vocab_freqs = data_utils.sort_vocab_by_frequency(vocab_freqs)

  # Limit vocab size
  ordered_vocab_freqs = ordered_vocab_freqs[:MAX_VOCAB_SIZE]

  # Add EOS token
  ordered_vocab_freqs.append((data_utils.EOS_TOKEN, 1))
上一篇下一篇

猜你喜欢

热点阅读