python - 无法使用 python 查找元音计数

标签 python python-3.x list

我正在尝试找出 ID 号,其中正文中的元音数量为 URL 的偶数:https://jsonplaceholder.typicode.com/posts

我的代码如下:

import json

import requests

   
url ='https://jsonplaceholder.typicode.com/posts'
content = requests.get(url).content

j = json.loads(content)
for each in j:

        print(each['id'],each['body'])

我现在可以打印每个用户 ID 的正文,但无法找出正文中元音的数量(偶数)。需要帮助

最佳答案

您可以使用以下代码计算元音:

print(*map(each['body'].lower().count, "aeiou"))

完整代码:

import json

import requests

   
url ='https://jsonplaceholder.typicode.com/posts'
content = requests.get(url).content

id_even = []
j = json.loads(content)
for each in j:
    cnt_vwl = 0
    for c in "aeiou":
        cnt_vwl += each['body'].lower().count(c)
    if cnt_vwl%2==0:
        id_even.append(each['id'])
id_even

输出:(id that every['body'] 有偶数元音)

[1, 3, 4, 5, 6, 7, 10,...]

关于python - 无法使用 python 查找元音计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69176624/

相关文章:

python - 使用 python (django) 进行 AWS Elastic Beanstalk 日志记录

Python 3.3+ : How to suppress exceptions in subprocess. Popen()?

python - 动态生成具有唯一名称的字典条目

list - 从列表中删除整数

python - 从 python 调用非 python 程序?

python - 如何从 C-API 按名称调用 python 函数?

python - 从 scipy.linalg 导入 _fblas : ImportError: DLL load failed: The specified module could not be found

python - 如何向按钮添加 Shift-Click 选项

list - Prolog - 检查出现次数未按预期工作

list - Kotlin:如何使用列表强制转换:未经检查的强制转换:kotlin.collections.List<Kotlin.Any?> 到 kotlin.colletions.List<Waypoint>