Xóa chuỗi trong chuỗi Python

Bài viết này trình bày một vấn đề như vậy về việc xóa ký tự thứ i khỏi chuỗi và nói về các giải pháp khả thi có thể được sử dụng để đạt được chúng bằng Python

Xóa ký tự thứ i khỏi chuỗi bằng phương thức gốc

Trong phương pháp này, người ta chỉ cần chạy một vòng lặp Python và nối thêm các ký tự khi chúng xuất hiện và tạo một chuỗi mới từ chuỗi hiện có trừ khi chỉ mục là i.  

Python3




The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
30
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
32

 

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
33

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
2

 

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
4
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
5
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
6
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
8
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
9

The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
1
The string after removal of i'th character : GeksForGeeks
2
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character : GeksForGeeks
4
The string after removal of i'th character : GeksForGeeks
5

The string after removal of i'th character : GeksForGeeks
6
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
1

 

The string after removal of i'th character : GeksForGeeks
2

The string after removal of i'th character : GeksForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character : GeksForGeeks
5
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
7

đầu ra

The string after removal of i'th character : GeksForGeeks

Xóa ký tự thứ i khỏi chuỗi bằng cách sử dụng str. thay thế()

các str. replace() có thể được sử dụng để thực hiện tác vụ xóa vì chúng ta có thể thay thế chỉ mục cụ thể bằng char trống và do đó giải quyết được sự cố.  

Hạn chế. Hạn chế chính của phương pháp này là nó không thành công trong trường hợp có các bản sao trong một chuỗi khớp với char tại vị trí. tôi. replace() thay thế tất cả các lần xuất hiện của một ký tự cụ thể và do đó sẽ thay thế tất cả các lần xuất hiện của tất cả các ký tự tại vị trí i. Đôi khi chúng ta vẫn có thể sử dụng chức năng này nếu ký tự thay thế xuất hiện lần đầu tiên trong chuỗi.  

Python3




The string after removal of i'th character : GeksForGeeks
8

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
30
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
32

 

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
33

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
73

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
76____277
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
78

 

The string after removal of i'th character : GeksForGeeks
2

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
30

The string after removal of i'th character : GeksForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
33
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
7

 

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
36

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
37

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0______131
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
76____1301
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
302
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
303
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
304

 

The string after removal of i'th character : GeksForGeeks
2

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
306

The string after removal of i'th character : GeksForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
309
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
7

đầu ra

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks

Xóa ký tự thứ i khỏi chuỗi bằng cách sử dụng lát cắt + nối

Người ta có thể sử dụng cắt chuỗi và cắt chuỗi trước pos i và cắt sau pos i. Sau đó, sử dụng phép nối chuỗi của cả hai, ký tự thứ i có thể bị xóa khỏi chuỗi.  

Python3




The string after removal of i'th character : GeksForGeeks
8

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
30
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
32

 

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
33

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
317

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0______131
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
320
The string after removal of i'th character : GeksForGeeks
4
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
322
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
324
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
325
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
326

 

The string after removal of i'th character : GeksForGeeks
2

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
328

The string after removal of i'th character : GeksForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character : GeksForGeeks
5
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
7

đầu ra

The string after removal of i'th character : GeksForGeeks

Xóa ký tự thứ i khỏi chuỗi bằng cách sử dụng str. tham gia () và hiểu danh sách

Trong phương pháp này, trước tiên, mỗi phần tử của chuỗi được chuyển đổi thành từng phần tử của danh sách, sau đó mỗi phần tử trong số chúng được nối để tạo thành một chuỗi ngoại trừ chỉ mục đã chỉ định.  

Python3




The string after removal of i'th character : GeksForGeeks
8

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
30
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
32

 

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
33

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
339

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
02
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
4
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
5
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
6
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
8
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
09
The string after removal of i'th character : GeksForGeeks
1
The string after removal of i'th character : GeksForGeeks
2
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character : GeksForGeeks
4
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
14

 

The string after removal of i'th character : GeksForGeeks
2

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
328

The string after removal of i'th character : GeksForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character : GeksForGeeks
5
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
7

đầu ra.  

The string after removal of i'th character : GeksForGeeks

Xóa ký tự cụ thể khỏi chuỗi bằng translate()

Trong phương pháp này, chúng tôi đã xóa 123 khỏi Geeksforgeeks bằng cách sử dụng chuỗi. Phiên dịch().  

Python3




The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
22
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
24

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
25

The string after removal of i'th character : GeksForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
22
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
29
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
30
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
32
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
4
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
5
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
36
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
37

đầu ra

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7

Sử dụng đệ quy

Để sử dụng đệ quy để xóa ký tự thứ i khỏi một chuỗi, bạn có thể định nghĩa một hàm đệ quy nhận vào chuỗi và chỉ mục cần xóa làm đối số. Hàm có thể kiểm tra xem chỉ mục có bằng 0 hay không, trong trường hợp đó, hàm sẽ trả về chuỗi đã xóa ký tự đầu tiên. Nếu chỉ số không phải là 0, hàm có thể trả về ký tự đầu tiên của chuỗi được nối với kết quả của việc gọi lại hàm trên chuỗi có chỉ số giảm đi 1

Đây là một ví dụ về cách bạn có thể thực hiện phương pháp này

Python3




The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
38
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
39

The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
41

The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character : GeksForGeeks
1
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
4
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
47
The string after removal of i'th character : GeksForGeeks
5

The string after removal of i'th character : GeksForGeeks
6
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
50
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
51
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
303
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
326

The string after removal of i'th character : GeksForGeeks
0

The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
56

The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
50
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
51
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
47
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
322
The string after removal of i'th character : GeksForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
63
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
303
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
65
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
66
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
303
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
304

 

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
69

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
30
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
32

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
0
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
31
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
75
The string after removal of i'th character : GeksForGeeks
4
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
304

The string after removal of i'th character : GeksForGeeks
3
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
7
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
80
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
81

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
82

Đầu ra

The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks
3

Xét về độ phức tạp về thời gian, cách tiếp cận này có độ phức tạp về thời gian là O(n) trong đó n là độ dài của chuỗi, bởi vì hàm thực hiện một lệnh gọi đệ quy duy nhất cho mỗi ký tự trong chuỗi

Về độ phức tạp của không gian, cách tiếp cận này có độ phức tạp của không gian là O(n) vì hàm thực hiện lệnh gọi đệ quy cho từng ký tự trong chuỗi và mỗi lệnh gọi yêu cầu không gian trên ngăn xếp cuộc gọi