Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Method #1 : Using loop
This is one of the ways in which this task can be performed. In this, we iterate through the list and then check for non presence of vowels and filter.
Python3
Bàn luận
Đôi khi, trong khi làm việc với Python, chúng ta có thể gặp vấn đề trong đó chúng ta muốn loại bỏ tất cả các nguyên âm không khỏi chuỗi. Đây là câu hỏi và giải pháp khá phổ biến cho nó rất hữu ích trong lập trình cạnh tranh và lập trình hàng ngày. Hãy thảo luận về một số cách nhất định trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại thông qua danh sách và sau đó kiểm tra không có sự hiện diện của nguyên âm và bộ lọc. & NBSP;
test_str
=
"Gfg is best for geeks"
print
[
"The original string is : "
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
=
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
eeeoee - A oue iee oa o ee5
eeeoee - A oue iee oa o ee6
The original string is : Gfg is best for geeks String after consonants removal : ieoee6
eeeoee - A oue iee oa o ee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee6
The original string is : Gfg is best for geeks String after consonants removal : ieoee7
The original string is : Gfg is best for geeks String after consonants removal : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee6
The original string is : Gfg is best for geeks String after consonants removal : ieoee7
eeeoee - A oue iee oa o ee3
eeeoee - A oue iee oa o ee4
The original string is : Gfg is best for geeks
String after consonants removal : ieoee
2=
eeeoee - A oue iee oa o ee
1
The original string is : Gfg is best for geeks String after consonants removal : ieoee
print
[
eeeoee - A oue iee oa o ee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
eeeoee - A oue iee oa o ee6
eeeoee - A oue iee oa o ee7
Method #2 : Using list comprehension
This is one of the ways in which this task can be performed. In this, we iterate through the list and then filter out vowels in similar manner but in one-liner.
Python3
Bàn luận
Đôi khi, trong khi làm việc với Python, chúng ta có thể gặp vấn đề trong đó chúng ta muốn loại bỏ tất cả các nguyên âm không khỏi chuỗi. Đây là câu hỏi và giải pháp khá phổ biến cho nó rất hữu ích trong lập trình cạnh tranh và lập trình hàng ngày. Hãy thảo luận về một số cách nhất định trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại thông qua danh sách và sau đó kiểm tra không có sự hiện diện của nguyên âm và bộ lọc. & NBSP;
test_str
=
"Gfg is best for geeks"
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee6
The original string is : Gfg is best for geeks String after consonants removal : ieoee7
eeeoee - A oue iee oa o ee3
eeeoee - A oue iee oa o ee4
The original string is : Gfg is best for geeks
String after consonants removal : ieoee
2=
eeeoee - A oue iee oa o ee
1
The original string is : Gfg is best for geeks String after consonants removal : ieoee
print
[
eeeoee - A oue iee oa o ee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
eeeoee - A oue iee oa o ee6
eeeoee - A oue iee oa o ee7
Đầu raO[n]
& nbsp; Phương pháp #2: Sử dụng danh sách hiểu & nbsp; Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại thông qua danh sách và sau đó lọc các nguyên âm theo cách tương tự nhưng trong một liner. & Nbsp;O[n]
Trong bài viết này, chúng tôi sẽ khám phá nhiều cách khác nhau để chúng tôi có thể loại bỏ tất cả các phụ âm khỏi một chuỗi nhất định trong Python. Cách đơn giản nhất sẽ là chuyển đổi một chuỗi thành danh sách và thay thế ký tự bằng '0' nếu nó không phải là nguyên âm và kết hợp tất cả các phần tử khác không.
Bạn có thể xóa các chữ cái khỏi một chuỗi trong Python không?
Bạn có thể xóa một ký tự khỏi chuỗi python bằng cách sử dụng thay thế [] hoặc dịch []. Cả hai phương thức này thay thế một ký tự hoặc chuỗi bằng một giá trị đã cho. Nếu một chuỗi trống được chỉ định, ký tự hoặc chuỗi bạn chọn sẽ bị xóa khỏi chuỗi mà không cần thay thế.
Trong bài viết này, chúng tôi sẽ khám phá nhiều cách khác nhau để chúng tôi có thể loại bỏ tất cả các phụ âm khỏi một chuỗi nhất định trong Python. Cách đơn giản nhất sẽ là chuyển đổi một chuỗi thành danh sách và thay thế ký tự bằng '0' nếu nó không phải là nguyên âm và kết hợp tất cả các phần tử khác không.
Bạn có thể xóa các chữ cái khỏi một chuỗi trong Python không?
Bạn có thể xóa một ký tự khỏi chuỗi python bằng cách sử dụng thay thế [] hoặc dịch []. Cả hai phương thức này thay thế một ký tự hoặc chuỗi bằng một giá trị đã cho. Nếu một chuỗi trống được chỉ định, ký tự hoặc chuỗi bạn chọn sẽ bị xóa khỏi chuỗi mà không cần thay thế.
Làm thế nào để bạn loại bỏ một chữ cái từ một chuỗi?
Examples:
Chúng ta có thể sử dụng hàm thay thế chuỗi [] để thay thế một ký tự bằng một ký tự mới. Nếu chúng tôi cung cấp một chuỗi trống làm đối số thứ hai, thì ký tự sẽ bị xóa khỏi chuỗi. str= “Welcome to geeksforgeeks”
Output: eoe o eeoeeXem thảo luận str= “What is your name?”
Output: a i ou ae?
Cải thiện bài viết Traverse all the characters of the string, if the character is a consonant then remove it from the final answer.
Lưu bài viết
Java
"Gfg is best for geeks"
1 =
81
=
82
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"Gfg is best for geeks"
4 =
85=
88print
9 =
90=
91
The original string is : Gfg is best for geeks String after consonants removal : ieoee09
The original string is : Gfg is best for geeks String after consonants removal : ieoee50
The original string is : Gfg is best for geeks String after consonants removal : ieoee51
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee64
"Gfg is best for geeks"
4 The original string is : Gfg is best for geeks String after consonants removal : ieoee66
The original string is : Gfg is best for geeks String after consonants removal : ieoee67
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee21
eeeoee - A oue iee oa o ee20
"Gfg is best for geeks"
16eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee21
eeeoee - A oue iee oa o ee20
"Gfg is best for geeks"
16The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8"Gfg is best for geeks"
1 "Gfg is best for geeks"
18
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0eeeoee - A oue iee oa o ee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee05
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0eeeoee - A oue iee oa o ee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee29
eeeoee - A oue iee oa o ee5
print
9 "Gfg is best for geeks"
27=
91The original string is : Gfg is best for geeks String after consonants removal : ieoee09__150150151
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee64
"Gfg is best for geeks"
4 The original string is : Gfg is best for geeks String after consonants removal : ieoee66
"Gfg is best for geeks"
38Độ phức tạp về thời gian: O [n] trong đó n là chiều dài của không gian chuỗi: O [1]
The original string is : Gfg is best for geeks String after consonants removal : ieoee44
The original string is : Gfg is best for geeks String after consonants removal : ieoee45
The original string is : Gfg is best for geeks String after consonants removal : ieoee44
The original string is : Gfg is best for geeks String after consonants removal : ieoee47
The original string is : Gfg is best for geeks String after consonants removal : ieoee48
The original string is : Gfg is best for geeks String after consonants removal : ieoee09
The original string is : Gfg is best for geeks String after consonants removal : ieoee50
The original string is : Gfg is best for geeks String after consonants removal : ieoee51
The original string is : Gfg is best for geeks String after consonants removal : ieoee44
The original string is : Gfg is best for geeks String after consonants removal : ieoee53
print
8"The original string is : "
8
eeeoee - A oue iee oa o ee5
"The original string is : "
8Làm thế nào để bạn loại bỏ tất cả các phụ âm khỏi một chuỗi trong Python?
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee64
"Gfg is best for geeks"
4 The original string is : Gfg is best for geeks String after consonants removal : ieoee66
The original string is : Gfg is best for geeks String after consonants removal : ieoee67
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0eeeoee - A oue iee oa o ee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee71
print
8
The original string is : Gfg is best for geeks String after consonants removal : ieoee73
The original string is : Gfg is best for geeks String after consonants removal : ieoee74
[
1eeeoee - A oue iee oa o ee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee77
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8"The original string is : "
8
Python3
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee21
eeeoee - A oue iee oa o ee20
"Gfg is best for geeks"
16"Gfg is best for geeks"
1 "Gfg is best for geeks"
18
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0eeeoee - A oue iee oa o ee5
print
9 The original string is : Gfg is best for geeks String after consonants removal : ieoee95
[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
9 eeeoee - A oue iee oa o ee13
[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee81
eeeoee - A oue iee oa o ee16
eeeoee - A oue iee oa o ee6
The original string is : Gfg is best for geeks String after consonants removal : ieoee92
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee20
=
eeeoee - A oue iee oa o ee222222
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee444____5
eeeoee - A oue iee oa o ee46
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
eeeoee - A oue iee oa o ee49
The original string is : Gfg is best for geeks String after consonants removal : ieoee7
eeeoee - A oue iee oa o ee51
[
eeeoee - A oue iee oa o ee53
[
eeeoee - A oue iee oa o ee6
eeeoee - A oue iee oa o ee56
eeeoee - A oue iee oa o ee5
eeeoee - A oue iee oa o ee58
=
eeeoee - A oue iee oa o ee13
eeeoee - A oue iee oa o ee61
eeeoee - A oue iee oa o ee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
eeeoee - A oue iee oa o ee64
The original string is : Gfg is best for geeks String after consonants removal : ieoee7
eeeoee - A oue iee oa o ee51
[
eeeoee - A oue iee oa o ee53
eeeoee - A oue iee oa o ee69
‘
The original string is : Gfg is best for geeks String after consonants removal : ieoee44
eeeoee - A oue iee oa o ee58
=
The original string is : Gfg is best for geeks String after consonants removal : ieoee95
[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee44
eeeoee - A oue iee oa o ee84
eeeoee - A oue iee oa o ee85
eeeoee - A oue iee oa o ee5
eeeoee - A oue iee oa o ee0
[
eeeoee - A oue iee oa o ee89
"Gfg is best for geeks"
6eeeoee - A oue iee oa o ee6
eeeoee - A oue iee oa o ee929__
‘
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
9 eeeoee - A oue iee oa o ee03
eeeoee - A oue iee oa o ee0
eeeoee - A oue iee oa o ee05
=
=
____eeeoee - A oue iee oa o ee08
eeeoee - A oue iee oa o ee09
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee6
=
The original string is : Gfg is best for geeks String after consonants removal : ieoee74
[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
eeeoee - A oue iee oa o ee17
eeeoee - A oue iee oa o ee6
eeeoee - A oue iee oa o ee19
C#
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee21
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee23
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee25
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee27
"Gfg is best for geeks"
1
eeeoee - A oue iee oa o ee29
"Gfg is best for geeks"
4
eeeoee - A oue iee oa o ee31
"Gfg is best for geeks"
6"Gfg is best for geeks"
7 "Gfg is best for geeks"
8print
0
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee0
print
3print
4 print
5print
6eeeoee - A oue iee oa o ee8
eeeoee - A oue iee oa o ee5
print
9 [
0[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee0
print
3[
5 print
5[
7eeeoee - A oue iee oa o ee8
eeeoee - A oue iee oa o ee5
print
9 [
0[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee0
print
3[
5 print
5[
7eeeoee - A oue iee oa o ee8
"The original string is : "
8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
9 "The original string is : "
5[
1print
0
"Gfg is best for geeks"
4
eeeoee - A oue iee oa o ee64
eeeoee - A oue iee oa o ee16
eeeoee - A oue iee oa o ee64
eeeoee - A oue iee oa o ee67
Các
Các
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee64
eeeoee - A oue iee oa o ee95
The original string is : Gfg is best for geeks String after consonants removal : ieoee50
[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
[
The original string is : Gfg is best for geeks String after consonants removal : ieoee37
test_str
02eeeoee - A oue iee oa o ee5
eeeoee - A oue iee oa o ee31
test_str
07"The original string is : "
5[
1eeeoee - A oue iee oa o ee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
[
The original string is : Gfg is best for geeks String after consonants removal : ieoee37
test_str
14eeeoee - A oue iee oa o ee5
print
0print
8print
0
The original string is : Gfg is best for geeks String after consonants removal : ieoee44
test_str
07[
0[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee44
eeeoee - A oue iee oa o ee84
[
1print
8"The original string is : "
8
eeeoee - A oue iee oa o ee5
"The original string is : "
8print
8
eeeoee - A oue iee oa o ee0
test_str
19eeeoee - A oue iee oa o ee5
print
0print
8test_str
39
eeeoee - A oue iee oa o ee5
"The original string is : "
8The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
9 eeeoee - A oue iee oa o ee03
"The original string is : "
8
eeeoee - A oue iee oa o ee0
eeeoee - A oue iee oa o ee05
=
=
____eeeoee - A oue iee oa o ee08
eeeoee - A oue iee oa o ee09
print
0
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee6
=
The original string is : Gfg is best for geeks String after consonants removal : ieoee74
[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee9
test_str
61eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee21
"The original string is : "
8
eeeoee - A oue iee oa o ee
20 eeeoee - A oue iee oa o ee
23
test_str
64
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee25
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee27
"Gfg is best for geeks"
1
eeeoee - A oue iee oa o ee29
"Gfg is best for geeks"
4
eeeoee - A oue iee oa o ee31
"Gfg is best for geeks"
6"Gfg is best for geeks"
7 "Gfg is best for geeks"
8test_str
65"The original string is : "
8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee0
print
3print
4 print
5print
6eeeoee - A oue iee oa o ee8
eeeoee - A oue iee oa o ee5
print
9 [
0[
1The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee0
print
3[
5 print
5[
7eeeoee - A oue iee oa o ee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
9 "The original string is : "
5[
1"Gfg is best for geeks"
4
eeeoee - A oue iee oa o ee64
eeeoee - A oue iee oa o ee16
eeeoee - A oue iee oa o ee64
eeeoee - A oue iee oa o ee67
Các
Các
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee64
eeeoee - A oue iee oa o ee95
The original string is : Gfg is best for geeks String after consonants removal : ieoee50
[
1=
45test_str
07[
0[
1
=
45
eeeoee - A oue iee oa o ee84
[
1print
8"The original string is : "
8
=
32"The original string is : "
8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
[
The original string is : Gfg is best for geeks String after consonants removal : ieoee37
test_str
02print
8test_str
39
=
32"The original string is : "
8
eeeoee - A oue iee oa o ee5
"The original string is : "
8eeeoee - A oue iee oa o ee5
eeeoee - A oue iee oa o ee31
test_str
07"The original string is : "
5[
1eeeoee - A oue iee oa o ee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
[
The original string is : Gfg is best for geeks String after consonants removal : ieoee37
test_str
14eeeoee - A oue iee oa o ee5
print
0test_str
65
The original string is : Gfg is best for geeks String after consonants removal : ieoee74
[
1test_str
65=
77
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
=
79Output:
eeeoee - A oue iee oa o ee
print
8eeeoee - A oue iee oa o ee
0 test_str
19O[N]
Auxiliary Space: O[N]
eeeoee - A oue iee oa o ee5
eeeoee - A oue iee oa o ee0
test_str
35The original string is : Gfg is best for geeks
String after consonants removal : ieoee
64 "Gfg is best for geeks"
4 The original string is : Gfg is best for geeks
String after consonants removal : ieoee
66 test_str
51eeeoee - A oue iee oa o ee
644____453
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
eeeoee - A oue iee oa o ee64
test_str
57The original string is : Gfg is best for geeks String after consonants removal : ieoee74
[
1"The original string is : "
8
JavaScript
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0test_str
65test_str
66 test_str
67
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8eeeoee - A oue iee oa o ee5
eeeoee - A oue iee oa o ee0
print
3test_str
71 print
5test_str
73__The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0eeeoee - A oue iee oa o ee5
"Gfg is best for geeks"
05The original string is : Gfg is best for geeks String after consonants removal : ieoee74
[
1eeeoee - A oue iee oa o ee5
"Gfg is best for geeks"
09The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8"The original string is : "
8
C#
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee21
eeeoee - A oue iee oa o ee20
eeeoee - A oue iee oa o ee23
"Gfg is best for geeks"
1 "Gfg is best for geeks"
18
print
0
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"Gfg is best for geeks"
4 =
85The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0eeeoee - A oue iee oa o ee5
print
9 "Gfg is best for geeks"
27=
91The original string is : Gfg is best for geeks String after consonants removal : ieoee09__150150151
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee64
"Gfg is best for geeks"
4 The original string is : Gfg is best for geeks String after consonants removal : ieoee66
"Gfg is best for geeks"
38The original string is : Gfg is best for geeks String after consonants removal : ieoee9
print
0eeeoee - A oue iee oa o ee5
"Gfg is best for geeks"
05The original string is : Gfg is best for geeks String after consonants removal : ieoee74
[
1eeeoee - A oue iee oa o ee5
"Gfg is best for geeks"
46The original string is : Gfg is best for geeks String after consonants removal : ieoee9
"The original string is : "
8"The original string is : "
8
Output:
eeeoee - A oue iee oa o ee
Độ phức tạp về thời gian: O [n] trong đó n là chiều dài của không gian chuỗi: O [1] O[n] where n is the length of string
Auxiliary Space: O[1]