Lọc nguyên âm và phụ âm trong python hackerrank

Viết chương trình Python để Đếm các Nguyên âm và Phụ âm trong một Chuỗi bằng cách sử dụng các giá trị Vòng lặp For và ASCII với một ví dụ thực tế

Chương trình Python đếm số nguyên âm và phụ âm trong một chuỗi Ví dụ 1

Chương trình python này cho phép người dùng nhập một chuỗi. Tiếp theo, nó đếm tổng số nguyên âm và phụ âm trong chuỗi này bằng vòng lặp For. Trước tiên, chúng tôi đã sử dụng Python For Loop để lặp lại từng ký tự trong Chuỗi. Trong Vòng lặp For, chúng tôi đang sử dụng Câu lệnh If để kiểm tra ký tự Chuỗi có phải là a, e, i, o, u, A, E, I, O, U. Nếu đúng, hãy tăng giá trị nguyên âm, nếu không, hãy tăng giá trị phụ âm

# Python Program to Count Vowels and Consonants in a String

str1 = input["Please Enter Your Own String : "]
vowels = 0
consonants = 0

for i in str1:
    if[i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'
       or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U']:
        vowels = vowels + 1
    else:
        consonants = consonants + 1
 
print["Total Number of Vowels in this String = ", vowels]
print["Total Number of Consonants in this String = ", consonants]

Python đếm nguyên âm và phụ âm trong đầu ra chuỗi

Please Enter Your Own String : Hello WOrld
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  8
>>> 
Please Enter Your Own String : Python Programs
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  12

Chương trình đếm số nguyên âm và phụ âm trong một chuỗi Ví dụ 2

Trong chương trình này, chúng ta đang sử dụng hàm lower để che chuỗi thành Chữ thường. Bằng cách này, bạn chỉ có thể sử dụng a, e, i, o, u bên trong câu lệnh If của Python [tránh viết hoa]

Đây là hướng dẫn về Bộ lọc trong Python. Ở đây chúng tôi thảo luận về phần giới thiệu, các ví dụ khác nhau và cách sử dụng Hàm filter[] trong python. Bạn cũng có thể xem các bài viết sau để tìm hiểu thêm –

Trong bài viết này, chúng ta sẽ viết chương trình Python để kiểm tra xem ký tự nhập vào là nguyên âm hay phụ âm

Mã Python

Trong chương trình này, người dùng được yêu cầu nhập một ký tự. Chương trình kiểm tra xem ký tự nhập vào có phải là nguyên âm chữ thường hay chữ hoa không, nếu đúng thì in ra thông báo ký tự là Nguyên âm ngược lại in ra ký tự là Phụ âm

Dòng

iiii
9 là sai. Nó phải là
def eliminate_consonants[x]:
    for char in x:
        if char in 'aeiou':
            print[char,end = ""]
0. Điều này là do bạn cần kiểm tra xem ký tự cụ thể đó có trong danh sách các nguyên âm không. Ngoài ra, bạn cần phải
def eliminate_consonants[x]:
    for char in x:
        if char in 'aeiou':
            print[char,end = ""]
1 [trong python3] để in đầu ra dưới dạng
def eliminate_consonants[x]:
    for char in x:
        if char in 'aeiou':
            print[char,end = ""]
2 tất cả trong một dòng

Chương trình cuối cùng sẽ như thế nào

def eliminate_consonants[x]:
        vowels= ['a','e','i','o','u']
        for char in x:
            if char in vowels:
                print[char,end = ""]

eliminate_consonants['mississippi']

Và đầu ra sẽ là

iiii

Những cách khác bao gồm

  • Sử dụng

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    3 một chuỗi

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    

    Có vẻ đơn giản, câu lệnh

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    4 kiểm tra xem
    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    5 có tồn tại trong chuỗi
    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    6 không

  • Hiểu danh sách

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    3

    Việc hiểu danh sách này sẽ trả về một danh sách chỉ chứa các ký tự nếu ký tự đó nằm trong

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    6

  • Một biểu thức máy phát điện

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    5

    Gen exp này sẽ trả về một trình tạo hơn là sẽ chỉ trả về các ký tự nếu ký tự đó nằm trong

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    6

  • Biểu thức chính quy

    Bạn có thể sử dụng

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    9 để chỉ khám phá các nguyên âm trong chuỗi của mình. Mật mã

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    8

    sẽ trả về một danh sách các nguyên âm được tìm thấy trong chuỗi i. e.

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    30. Vì vậy, bây giờ chúng ta có thể sử dụng
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    31 và sau đó sử dụng

    iiii
    
    1
  • Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    32 và
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    33

    Đối với kỹ thuật này, bạn sẽ cần lưu trữ một bản đồ khớp từng nguyên âm không phải là nguyên âm với loại

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    34. Đối với điều này, bạn có thể sử dụng
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    35. Mã để tạo bản đồ là

    iiii
    
    6

    điều này sẽ trả về ánh xạ. Hãy lưu trữ nó trong một biến [ở đây là

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    36 cho bản đồ]

    iiii
    
    8

    Thao tác này sẽ xóa tất cả các ký tự không phải là

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    6 khỏi chuỗi

  • Sử dụng

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    38

    Bạn có thể sử dụng

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    38 cùng với
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    50. Nhưng hãy nhớ
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    51 trước

    def eliminate_consonants[x]:
        for char in x:
            if char in 'aeiou':
                print[char,end = ""]
    
    4

    và bây giờ sử dụng

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    32

    iiii
    
    0
  • Sử dụng

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    53

    Như đã đề cập bởi J. F. Sebastian trong các nhận xét bên dưới, bạn có thể tạo một chuỗi phụ âm chữ thường bằng cách sử dụng

    iiii
    
    1

    Sử dụng điều này, chúng ta có thể dịch từ,

    iiii
    
    2

    sẽ trả lại

    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    54. Điều này có thể dễ dàng được chuyển đổi thành
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    55 bằng cách sử dụng
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    56 i. e.
    Please Enter Your Own String : Hello WOrld
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  8
    >>> 
    Please Enter Your Own String : Python Programs
    Total Number of Vowels in this String =  3
    Total Number of Consonants in this String =  12
    57

Chủ Đề