按频率排序文件中的哈希标记并将其发送到另一个fi

2024-09-27 17:30:19 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我们的目标是使用一个充满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的第一次评估…真是太棒了


Tags: 文件inimport目标fordataline标签
3条回答

您可以使用collections模块并使用collections.Counter(list_of_hastags).most_common(# most common you want)返回文件中最常见的事件。在

或者,如果你不想限制,你甚至不需要传递最常见事件的数量。在

小例子:

import collections
#In your file this will likely be data.readlines() depending on how your file is struct.
#to get the list of hastags, you may need to split etc depending on structure
hashtags = ['#1', '#1', '#2', '#2', '#3', '#4', '#4']
print(collections.Counter(hashtags).most_common())

结果:

^{pr2}$

您可以使用^{}数据类型计算每个标签的频率,如下所示:

from collections import Counter

freq = Counter()
with open("twitter_data.txt") as data:
    for line in data:
        for part in line.split():
            if "#" in part:
                freq[part] += 1
print(freq.most_common())

根据问题和现有代码的结构,twitter_data.txt看起来像这样(每条tweet用newline分隔):

^{pr2}$

在此示例文件上运行上述代码将生成以下输出:

^{3}$

{a1在循环中定义了一个偶数参数,你可以在一个嵌套的参数中插入一个参数:

data="""
#1 hello #2
this is #2 a #3 test
#2 life is good #1""".split("\n")

import collections

hashtags = collections.Counter(part
                                for line in data
                                    for part in line.split()
                                        if "#" in part)

print(hashtags.most_common())

给我这个输出:

^{pr2}$

相关问题 更多 >

    热门问题