python统计句子中重复的单词,PYTHON:如何制作一个比较两个列表中重复出现的单词的函数?...

For example:

q=["hi", "sky"]

p=["here","sky","sky","sky","sky"]

What would the function be defined to get:

count_words(["hi", "sky"], ["here","sky","sky","sky","sky"])

[0, 4]

# answer where hi appears 0 times and sky appears 4 times

I started the code off like this:

def count_words(q, p):

count = 0

for word in q:

if q==p:

(q.count("hi"))

(q.count("sky"))

return count

I keep getting one value of 0, which accounts for q, but I can't get a value for p.

解决方案

Here is a simpler answer (by simpler I mean one-liner and no use of additional libraries) -

q=["hi", "sky"]

p=["here","sky","sky","sky","sky"]

def count_words(q,p):

return [ p.count(i) for i in q ]

print(count_words(q,p))

Output

[0, 4]

Explanation

[ p.count(i) for i in q ] is a list comprehension which is like iterating through the q list on the fly and counting for the respective elements in p

Timings (depends on the data)

# My solution

1.78 µs ± 214 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# @Delirious Solution 1

7.55 µs ± 1.58 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# @ Delirious Solution 2

3.86 µs ± 348 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

### 回答1: 可以使用 Python 内置的 `collections` 模块,调用它的 `Counter` 函数,它可以计算出一个单词同一字母的重复次数,比如:```python from collections import Counterword = 'mississippi' Counter(word)# 输出: # Counter({'i': 4, 's': 4, 'p': 2, 'm': 1}) ``` ### 回答2: 要查询一个单词同一字母的重复次数,可以使用Python的字典数据结构和计数功能来实现。以下是具体的解决方案: 1. 首先,定义一个空字典来存储每个字母的重复次数。 2. 使用一个循环遍历单词的每个字母。 3. 对于每个字母,首先使用字典的get()方法获取该字母的计数值(如果存在),并将其存储在一个临时变量。 4. 然后,使用字典的setdefault()方法来设置字母的计数值。如果字母已经存在于字典,那么计数值将不会被改变;如果字母不存在于字典,那么计数值将被设置为1。 5. 最后,遍历字典的每个键和值对,并打印出字母和它的计数值。 下面是一个示例代码: ```python def count_letter(word): letter_count = {} for letter in word: count = letter_count.get(letter, 0) letter_count[letter] = count + 1 for letter, count in letter_count.items(): print(f"{letter}: {count}") # 测试代码 count_letter("hello") ``` 运行以上代码,将会输出每个字母以及它在单词重复次数: ``` h: 1 e: 1 l: 2 o: 1 ``` 以上是使用Python查询一个单词同一字母的重复次数的方法。希望对你有帮助! ### 回答3: 要使用Python查询一个单词同一字母的重复次数,可以按照以下步骤操作: 1. 首先,定义一个函数来查询重复字母的次数。函数的输入参数应该是一个单词。 ```python def find_duplicate_letters(word): ``` 2. 然后,创建一个空的字典,用于存储每个字母和它们的重复次数。 ```python letter_counts = {} ``` 3. 接下来,使用一个循环遍历单词的每个字母。 ```python for letter in word: ``` 4. 在循环,检查当前字母是否已经在字典存在。如果存在,就将它的计数值加1;如果不存在,就在字典添加这个字母,并将计数值设置为1。 ```python if letter in letter_counts: letter_counts[letter] += 1 else: letter_counts[letter] = 1 ``` 5. 最后,返回字典每个字母的计数值。 ```python return letter_counts ``` 完整的代码如下: ```python def find_duplicate_letters(word): letter_counts = {} for letter in word: if letter in letter_counts: letter_counts[letter] += 1 else: letter_counts[letter] = 1 return letter_counts ``` 可以使用以下代码测试这个函数: ```python word = input("请输入一个单词:") result = find_duplicate_letters(word) print(result) ``` 例如,如果输入单词"hello",输出结果应为:{'h': 1, 'e': 1, 'l': 2, 'o': 1},表示字母'h'和'e'各出现一次,字母'l'出现了两次,字母'o'出现了一次。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值