【问题标题】:How to plot rank vs count using the list generated with "Counter"如何使用“计数器”生成的列表绘制排名与计数
【发布时间】:2021-08-31 15:50:32
【问题描述】:

我对编码很陌生,我试图首先使用计数器生成的列表来制作一个图,即单词的排名与计数的关系。 我的第一个问题是如何访问这些值。 most_frequent(white_token)[0] 给了我 ('the', 26423),但是如何访问值 26423,我猜它应该用于排名? (解决了) 第二个问题是,这个计数器命令似乎已经根据单词的频率进行了排名。但是我如何提取这些信息,例如我如何为单词“to”的排名获得 3

感谢您的帮助! 图片显示了计数器1的结果

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python counter rank


【解决方案1】:

基于此:

" most_frequent(white_token)[0] gives me ('the', 26423), but how to access the value 26423 "

如果

most_frequent(white_token)[0]

返回这个:

('the', 26423)

然后,当您使用“ most_frequent(white_token)[0]”时,它会返回一个元组。要索引一个元组,你可以像索引一个列表一样索引它。

例如:

most_frequent(white_token)[0]
# returns ('the', 26423)

most_frequent(white_token)[0][0]
# returns 'the'

most_frequent(white_token)[0][1]
# returns 26423

【讨论】:

    【解决方案2】:

    most_frequent 的数据类型为list,返回结果为tuple。 要访问元组,您需要按以下方式进行:

    most_frequent(white_token)[0][0]
    # Output
    'the'
    
    most_frequent(white_token)[0][1]
    # Output
    26423
    

    【讨论】:

      猜你喜欢
      • 2012-07-05
      • 2022-07-26
      • 2019-02-06
      • 2018-10-10
      • 2018-09-03
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多