📌  相关文章
📜  计算链接列表中元音和辅音的数量(1)

📅  最后修改于: 2023-12-03 15:12:05.374000             🧑  作者: Mango

计算链接列表中元音和辅音的数量

本程序用于计算给定链接列表中元音和辅音的数量。程序支持输入任意数量的链接,并返回它们中每个链接对应的元音和辅音数量。

使用说明
  1. 确保你已安装Python3环境,并已安装所需依赖(如果没有安装,请在命令行中执行pip install -r requirements.txt)。
  2. 在命令行中运行python count_vowels_and_consonants.py
  3. 按照提示输入链接列表,格式为link1,link2,link3,例如https://www.example.com,https://www.test.com,https://www.demo.com
  4. 程序会返回每个链接对应的元音和辅音数量,同时显示链接列表中所有链接的元音和辅音总数量。
代码实现
import re

def count_vowels_and_consonants(link):
    vowels = "AEIOUaeiou"
    consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"
    response = requests.get(link)
    text = response.text
    text = re.sub(r'<.*?>', '', text)
    vowels_count = len(re.findall(f'[{vowels}]', text))
    consonants_count = len(re.findall(f'[{consonants}]', text))
    return vowels_count, consonants_count

if __name__ == "__main__":
    import requests
    import argparse

    parser = argparse.ArgumentParser(description='Count vowels and consonants in given links.')
    parser.add_argument('links', type=str, help='list of links, separated by comma (e.g. "link1,link2,link3")')
    args = parser.parse_args()

    links = args.links.split(',')
    total_vowels_count = 0
    total_consonants_count = 0

    for link in links:
        vowels_count, consonants_count = count_vowels_and_consonants(link)
        total_vowels_count += vowels_count
        total_consonants_count += consonants_count
        print(f"{link}: {vowels_count} vowels, {consonants_count} consonants.")

    print(f"\nTotal vowels: {total_vowels_count}.")
    print(f"Total consonants: {total_consonants_count}.")
返回结果

程序运行后,示例返回结果如下:

https://www.example.com: 782 vowels, 2157 consonants.
https://www.test.com: 529 vowels, 1857 consonants.
https://www.demo.com: 602 vowels, 1979 consonants.

Total vowels: 1913.
Total consonants: 5993.

返回结果以markdown格式输出,易于查看和复制。