Hướng dẫn write a python program to split a string on the last occurrence of the delimiter. - viết chương trình python để tách một chuỗi ở lần xuất hiện cuối cùng của dấu phân cách.

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:43 [UTC/GMT +8 giờ]

Chuỗi Python: Tập thể dục-50 với giải pháp

Viết một chương trình Python để chia một chuỗi vào lần xuất hiện cuối cùng của dấu phân cách.

Giải pháp mẫu:-:-

Mã Python:

str1 = "w,3,r,e,s,o,u,r,c,e"
print[str1.rsplit[',', 1]]
print[str1.rsplit[',', 2]]
print[str1.rsplit[',', 5]]

Đầu ra mẫu:

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               

Flowchart:


Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Trình chỉnh sửa mã Python:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus.

Trước đó: Viết chương trình Python để đếm và hiển thị các nguyên âm của một văn bản đã cho. Write a Python program to count and display the vowels of a given text.
Next: Write a Python program to find the first non-repeating character in given string

Python: Lời khuyên trong ngày

Sử dụng năng suất để tạo ra một trình lặp đơn giản:

>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]

Việc chia các chuỗi luôn được thảo luận trong các ứng dụng và trường hợp sử dụng khác nhau. Một trong những biến thể thú vị của việc phân tách danh sách có thể được chia danh sách trên Delimiter nhưng lần này chỉ vào lần xuất hiện cuối cùng của nó. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện.

Phương pháp số 1: Sử dụng RSPLIT [STR, 1] Chuỗi phân chia chuỗi bình thường có thể thực hiện phân tách từ phía trước, nhưng Python cũng cung cấp một phương pháp khác có thể thực hiện chính nhiệm vụ này từ phía sau, do đó tăng tính linh hoạt của các ứng dụng. & NBSP; The normal string split can perform the split from the front, but Python also offers another method that can perform this very task from the rear end, hence increasing the versatility of applications. 

Python3

test_string = "gfg, is____1011

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
3
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
4
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
6
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
7

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
8=
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
0
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
1
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
2223
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
4

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
3
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
6
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
7
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
8
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
6
The original string : gfg, is, good, better, and best
The splitted list at the last comma : ['gfg, is, good, better', 'and best']
1

Đầu ra: & nbsp;

The original string : gfg, is, good, better, and best
The splitted list at the last comma : ['gfg, is, good, better', 'and best']

& nbsp; Phương pháp số 2: Sử dụng RPartition [] Hàm này cũng có thể thực hiện phân vùng ngược mong muốn, nhưng các nhược điểm đối với việc sử dụng đây là việc xây dựng giá trị phân cách bổ sung và tốc độ cũng chậm hơn phương thức trên và do đó không được khuyến nghị. & NBSP;Method #2: Using rpartition[] This function can also perform the desired reverse partition, but the drawbacks to using this is the construction of additional delimiter value and also the speed is slower than the above method and hence not recommended. 

Python3

test_string = "gfg, is____1011

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
3
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
4
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
6
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
7

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
8=
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
0
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
1
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
2223
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
4

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
3
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
6
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
7
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
8
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
6
The original string : gfg, is, good, better, and best
The splitted list at the last comma : ['gfg, is, good, better', 'and best']
1

Đầu ra: & nbsp;

The original string : gfg, is, good, better, and best
The splitted list at the last comma : ['gfg, is, good, better', ', ', 'and best']

& nbsp; Phương pháp số 2: Sử dụng RPartition [] Hàm này cũng có thể thực hiện phân vùng ngược mong muốn, nhưng các nhược điểm đối với việc sử dụng đây là việc xây dựng giá trị phân cách bổ sung và tốc độ cũng chậm hơn phương thức trên và do đó không được khuyến nghị. & NBSP; Using split[] and replace[] methods.

Python3

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
8=
The original string : gfg, is, good, better, and best
The splitted list at the last comma : ['gfg, is, good, better', ', ', 'and best']
6
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
1
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
4

Phương pháp số 3: Sử dụng các phương thức Split [] và thay thế [].

test_string 5=test_string 7test_string 8

>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
4

=0==2

=3==5

test_string =

The original string : gfg, is, good, better, and best
The splitted list at the last comma : ['gfg, is, good, better', ' and best']
8

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
3test_string 0test_string 1
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
6
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
7

is1=3

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5=is5

is1=0

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5=
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
3

"gfg, 0

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
02
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
03

is1=3

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5=
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
08

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
09=
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
11test_string 8
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
13

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
14=2
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
16=
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
14=2
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
20is5
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
22test_string 8
>>> def foo[lst]:
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list[foo[a]]
[1, 2, 3, 6]
4

['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
3test_string 0
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
27
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
5
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
6
['w,3,r,e,s,o,u,r,c', 'e']                                                                                    
['w,3,r,e,s,o,u,r', 'c', 'e']                                                                                 
['w,3,r,e,s', 'o', 'u', 'r', 'c', 'e']               
30

Đầu ra

The original string : gfg, is, good, better, and best
The splitted list at the last comma : ['gfg, is, good, better', ' and best']


Bài Viết Liên Quan

Chủ Đề