单词多久出现在列表中-Python

发布于 2025-02-09 08:37:25 字数 435 浏览 2 评论 0原文

我有一个单词列表。 我想出了如何计算每个单词的发生。 我现在想知道在该列表中出现了多少次(?)。输出应该看起来像这样:

4.500单词出现1次

6.000单词2次 ...

示例:

list = ["hello", "time", "burger", "hello", "mouse", "time", "time"]

预期输出:

3 Word occurred 1 time
1 Word orrcured 2 time
1 Word occurred 3 time

我希望我的意思是我的意思。我无法共享代码,因为我不知道该怎么做。

是否有可以做到这一点的内置功能?

还是有人有一种平稳的计算方式?

多谢 !

I have a list of words.
I figgured out how to count the occurrence of each word.
I now want to know how many words appear how many times(?) in that list. The output should look something like this:

4.500 Words appeard 1 time

6.000 Words appeard 2 time
...

Example:

list = ["hello", "time", "burger", "hello", "mouse", "time", "time"]

Expected output:

3 Word occurred 1 time
1 Word orrcured 2 time
1 Word occurred 3 time

I hope it is clear what I mean to do. I cant share code really since I have no clue how to do it.

Is there an inbuilt function with Counter or Pandas that can do this?

Or does anyone have a smooth way of computing this ?

THANKS A LOT !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡少年 2025-02-16 08:37:25

是的;使用计数器来计算每个单词出现的次数,然后在该计数器上使用counter来计数每次出现多少个单词。

>>> words = ["hello", "time", "burger", "hello", "mouse", "time", "time"]
>>> from collections import Counter
>>> word_counts = Counter(words)
>>> count_counts = Counter(word_counts.values())
>>> for times, words in count_counts.items():
...     print(f"{words} words occurred {times} times")
...
1 words occurred 2 times
1 words occurred 3 times
2 words occurred 1 times

请注意,word_counts给出每个单词的计数,然后count_counts给出word_counts

>>> word_counts
Counter({'time': 3, 'hello': 2, 'burger': 1, 'mouse': 1})
>>> count_counts
Counter({1: 2, 2: 1, 3: 1})

Yes; use a Counter to count the number of times each word appears, and then use a Counter on that counter to count how many words appear each number of times.

>>> words = ["hello", "time", "burger", "hello", "mouse", "time", "time"]
>>> from collections import Counter
>>> word_counts = Counter(words)
>>> count_counts = Counter(word_counts.values())
>>> for times, words in count_counts.items():
...     print(f"{words} words occurred {times} times")
...
1 words occurred 2 times
1 words occurred 3 times
2 words occurred 1 times

Note that word_counts gives the counts per word, and then count_counts gives the counts per count in word_counts:

>>> word_counts
Counter({'time': 3, 'hello': 2, 'burger': 1, 'mouse': 1})
>>> count_counts
Counter({1: 2, 2: 1, 3: 1})
时光暖心i 2025-02-16 08:37:25

如果您知道如何计算每个单词的发生,则该过程很简单。在伪代码中:

  1. a = n*[0],其中n是每个单词的单词量
  2. ,让m是单词发生的次数。然后do a [m] ++
  3. 在范围N中为i,print(str(a [i]) +“ shops epply” + str“ + str(i) +“ times”)

If you know how to count the occurrences of each word, the procedure is straightforward. In pseudo-code:

  1. Let A = n*[0], where n is the amount of words
  2. For each word, let m be the number of times the word occurs. Then do A[m]++
  3. For i in range n, print(str(A[i]) + " words appear " + str(i) + "times")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文