因此,我们的目标是使用一个充满tweets的文件,找到最常见的hashtags(#),并在另一个文件中按频率对它们进行排名 到目前为止我有这个
import collections
with open("/Users/Adnan/Desktop/twitter_data.txt") as data:
for line in data:
for part in line.split():
if "#" in part:
print(part)
print(collections.Counter(part).most_common())
它打印出文件中所有不同的标签。我该如何将它发送到另一个文件中,根据标签的流行程度根据它出现的频率进行排名?在
我一直在努力工作
^{pr2}$我接近它工作,但它继续窃听,真的非常需要帮助,所以如果有人可以感谢你。在
示例数据:@stellargirl I looooooovvvvveee我的Kindle2。不是说DX很酷,而是2本身就很棒。 正在阅读我的kindle2。。。喜欢它。。。李·查尔兹是个博学的人。 好吧,对kindle2的第一次评估…真是太棒了
您可以使用
collections
模块并使用collections.Counter(list_of_hastags).most_common(# most common you want)
返回文件中最常见的事件。在或者,如果你不想限制,你甚至不需要传递最常见事件的数量。在
小例子:
结果:
^{pr2}$您可以使用^{} 数据类型计算每个标签的频率,如下所示:
根据问题和现有代码的结构,
^{pr2}$twitter_data.txt
看起来像这样(每条tweet用newline分隔):在此示例文件上运行上述代码将生成以下输出:
^{3}${a1在循环中定义了一个偶数参数,你可以在一个嵌套的参数中插入一个参数:
给我这个输出:
^{pr2}$相关问题 更多 >
编程相关推荐