Python đếm nguyên âm trong danh sách

Từ phần giải thích trước, chúng tôi đã giới thiệu cách phát hiện nguyên âm trong một chuỗi nhất định. Chúng ta sẽ viết lại các ví dụ trước bằng cách áp dụng tiêu chí phụ âm thay vì điều kiện nguyên âm

Giả sử bạn đã biết điều kiện cần thiết để áp dụng để tìm nạp phụ âm

In các phụ âm trong một chuỗi bằng vòng lặp for

text = input['Enter text: ']
 
count = 0

for char in text:
    if char.lower[] not in 'aeiou':
    	print[char]   		
        count = count + 1
print["No. of consonants:", count]    
  

Output:

 Enter text: 'QuizCure'
Q
z
C
r
['No. of consonants:', 4]

Giải trình

  • Lặp qua chuỗi đầu vào của người dùng
  • Đã chuyển đổi từng phần tử lặp thành thấp hơn để tránh sự cố trường hợp
  • Kiểm tra xem chữ cái có thuộc nguyên âm 'aeiou' không
  • Chữ in thỏa mãn điều kiện
  • In tổng số phụ âm trong một chuỗi

In các phụ âm trong một chuỗi bằng cách sử dụng khả năng hiểu danh sách

text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 

Output:

Enter text: 'QuizCure'
4
['Q', 'z', 'C', 'r']
   

Giải trình. hiểu danh sách trở lại đây danh sách các phụ âm đáp ứng điều kiện

5
['e', 'e', 'o', 'e', 'e']
8 không có trong aeiou.
5
['e', 'e', 'o', 'e', 'e']
9 trả về độ dài của danh sách phụ âm

5
['e', 'e', 'o', 'e', 'e']
0_______40
5
['e', 'e', 'o', 'e', 'e']
1
5
['e', 'e', 'o', 'e', 'e']
2
5
['e', 'e', 'o', 'e', 'e']
3
5
['e', 'e', 'o', 'e', 'e']
4
5
['e', 'e', 'o', 'e', 'e']
5
5
['e', 'e', 'o', 'e', 'e']
6
5
['e', 'e', 'o', 'e', 'e']
7
5
['e', 'e', 'o', 'e', 'e']
4
5
['e', 'e', 'o', 'e', 'e']
5
Output:

 Enter text: 'QuizCure'
Q
z
C
r
['No. of consonants:', 4]
40

5
['e', 'e', 'o', 'e', 'e']
0______142____143
Output:

 Enter text: 'QuizCure'
Q
z
C
r
['No. of consonants:', 4]
44
Output:

 Enter text: 'QuizCure'
Q
z
C
r
['No. of consonants:', 4]
45

5
['e', 'e', 'o', 'e', 'e']
0____142____148

5
['e', 'e', 'o', 'e', 'e']
0

text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
10

5
['e', 'e', 'o', 'e', 'e']
6
5
['e', 'e', 'o', 'e', 'e']
1
text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
13

text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
14
5
['e', 'e', 'o', 'e', 'e']
1
text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
16

text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
17

Đầu ra.
 

5
['e', 'e', 'o', 'e', 'e']

 

đếm nguyên âm. từ điển cách

Điều này cũng thực hiện tác vụ tương tự nhưng theo một cách khác. Trong phương pháp này, chúng tôi tạo một từ điển với các nguyên âm và tăng chúng khi gặp một nguyên âm. Trong phương pháp này, chúng tôi sử dụng phương pháp gấp trường hợp để bỏ qua các trường hợp, theo đó chúng tôi tạo thành một từ điển các nguyên âm với khóa là một nguyên âm. Đây là cách tốt hơn và hiệu quả hơn để kiểm tra và tìm số lượng của từng nguyên âm có trong một chuỗi.
 

Python3




text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
18

text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
19

5
['e', 'e', 'o', 'e', 'e']
4
5
['e', 'e', 'o', 'e', 'e']
5

5
['e', 'e', 'o', 'e', 'e']
0

5
['e', 'e', 'o', 'e', 'e']
0
5
['e', 'e', 'o', 'e', 'e']
84

5
['e', 'e', 'o', 'e', 'e']
0______46
5
['e', 'e', 'o', 'e', 'e']
1
5
['e', 'e', 'o', 'e', 'e']
88

5
['e', 'e', 'o', 'e', 'e']
0

5
['e', 'e', 'o', 'e', 'e']
0
5
['e', 'e', 'o', 'e', 'e']
31

5
['e', 'e', 'o', 'e', 'e']
0
5
['e', 'e', 'o', 'e', 'e']
33

5
['e', 'e', 'o', 'e', 'e']
0______635
5
['e', 'e', 'o', 'e', 'e']
1
5
['e', 'e', 'o', 'e', 'e']
37
5
['e', 'e', 'o', 'e', 'e']
38
5
['e', 'e', 'o', 'e', 'e']
39

5
['e', 'e', 'o', 'e', 'e']
0

5
['e', 'e', 'o', 'e', 'e']
0
5
['e', 'e', 'o', 'e', 'e']
32

5
['e', 'e', 'o', 'e', 'e']
0
5
['e', 'e', 'o', 'e', 'e']
3
5
['e', 'e', 'o', 'e', 'e']
35
5
['e', 'e', 'o', 'e', 'e']
5
5
['e', 'e', 'o', 'e', 'e']
37

5
['e', 'e', 'o', 'e', 'e']
38
5
['e', 'e', 'o', 'e', 'e']
7
5
['e', 'e', 'o', 'e', 'e']
35______45
5
['e', 'e', 'o', 'e', 'e']
42

5
['e', 'e', 'o', 'e', 'e']
43
5
['e', 'e', 'o', 'e', 'e']
44____545
5
['e', 'e', 'o', 'e', 'e']
1
5
['e', 'e', 'o', 'e', 'e']
47

5
['e', 'e', 'o', 'e', 'e']
0
5
['e', 'e', 'o', 'e', 'e']
49
5
['e', 'e', 'o', 'e', 'e']
35

5
['e', 'e', 'o', 'e', 'e']
0

text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
10

text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
14
5
['e', 'e', 'o', 'e', 'e']
1
5
['e', 'e', 'o', 'e', 'e']
55

5
['e', 'e', 'o', 'e', 'e']
6
5
['e', 'e', 'o', 'e', 'e']
1
text = input['Enter text: ']
count = 0

consonants = [char for char in text if char.lower[] not in "aeiou"]

print[len[consonants]]   
print[consonants]   
 
13

Output:

 Enter text: 'QuizCure'
Q
z
C
r
['No. of consonants:', 4]
42
5
['e', 'e', 'o', 'e', 'e']
00

Đầu ra.
 

Output:

 Enter text: 'QuizCure'
Q
z
C
r
['No. of consonants:', 4]
4

đếm nguyên âm. cách biểu thức chính quy

Chúng ta cũng có thể sử dụng phương pháp này để thực hiện nhiệm vụ này. Chúng ta có thể sử dụng biểu thức chính quy để thực hiện tác vụ này. chúng tôi sử dụng lại. phương thức findall[] để tìm tất cả các nguyên âm trong chuỗi tạo danh sách với chúng. Chúng tôi sử dụng len trên danh sách để tìm tổng số nguyên âm trong chuỗi.  

Chủ Đề