我正在尝试找出 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/