Có thể xóa chuỗi văn bản khỏi các ô khác nếu chúng lặp lại trong ô trước đó không? . Tôi đang cố giữ lại lần xuất hiện đầu tiên của một chuỗi nhưng xóa chuỗi đó khỏi các ô khác
Ví dụ: tôi muốn xóa bất kỳ chuỗi nào lặp lại và dài hơn 2 ký tự
abc
def
ghi
jkl
abdgj
adetyk
Vì "ab" nằm ở ô đầu tiên và cũng là ô thứ 5 nên có thể xóa "ab" ở ô thứ 5 được không?
"de" cũng lặp lại ở ô thứ 2 và thứ 6, "de" chỉ còn lại ở ô thứ 2 nhưng bị xóa ở ô thứ 6. Ô thứ 2 sẽ vẫn là "def" nhưng ô thứ 6 sẽ chuyển thành "atyk"
Việc thay thế ký tự này bằng ký tự khác là một vấn đề phổ biến mà mọi lập trình viên Python trước đây đều phải làm việc. Nhưng đôi khi, chúng tôi yêu cầu giải pháp một dòng đơn giản có thể thực hiện tác vụ cụ thể này. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Phương pháp 1. Thay thế nhiều ký tự bằng cách sử dụng thay thế lồng nhau ()
Vấn đề này có thể được giải quyết bằng phương pháp thay thế lồng nhau, bên trong sẽ tạo ra một. biến để giữ trạng thái thay thế trung gian.
Python3
The original string is : abbabba The string after replacement of positions : baabaab0
The original string is : abbabba The string after replacement of positions : baabaab1The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab4
The original string is : abbabba The string after replacement of positions : baabaab5The original string is : abbabba The string after replacement of positions : baabaab6The original string is : abbabba The string after replacement of positions : baabaab0 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab2The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab4
The original string is : abbabba The string after replacement of positions : baabaab5
The original string is : abbabba The string after replacement of positions : baabaab6The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab8The original string is : abbabba The string after replacement of positions : baabaab9The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba1The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba2The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba3The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0The original string is : abbabba The string after replacement of positions : baabaab9The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba2The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba1The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba3The original string is : abbabba The string after replacement of positions : baabaab00
The original string is : abbabba The string after replacement of positions : baabaab01
The original string is : abbabba The string after replacement of positions : baabaab5The original string is : abbabba The string after replacement of positions : baabaab6The original string is : abbabba The string after replacement of positions : baabaab04 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab06
đầu ra
The original string is : abbabba The string after replacement of positions : baabaabPhương pháp 2. Thay thế nhiều ký tự bằng translate() + maketrans()
Ngoài ra còn có một chức năng cống hiến có thể thực hiện loại nhiệm vụ thay thế này trong một dòng duy nhất, do đó đây là cách được đề xuất để giải quyết vấn đề cụ thể này. Chỉ hoạt động trong Python2.
Python3
The original string is : abbabba The string after replacement of positions : baabaab07 The original string is : abbabba The string after replacement of positions : baabaab08
The original string is : abbabba The string after replacement of positions : baabaab0
The original string is : abbabba The string after replacement of positions : baabaab1The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab4
The original string is : abbabba The string after replacement of positions : baabaab5The original string is : abbabba The string after replacement of positions : baabaab6The original string is : abbabba The string after replacement of positions : baabaab0 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab2The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab20
The original string is : abbabba The string after replacement of positions : baabaab5
The original string is : abbabba The string after replacement of positions : baabaab6The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab24____325The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0The original string is : abbabba The string after replacement of positions : baabaab27The original string is : abbabba The string after replacement of positions : baabaab28
The original string is : abbabba The string after replacement of positions : baabaab01
The original string is : abbabba The string after replacement of positions : baabaab5The original string is : abbabba The string after replacement of positions : baabaab6The original string is : abbabba The string after replacement of positions : baabaab04 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab06
đầu ra
The original string is : abbabba The string after replacement of positions : baabaabPhương pháp 3. Thay thế nhiều ký tự bằng cách sử dụng
subn() tương tự như sub() về mọi mặt, ngoại trừ cách cung cấp đầu ra. Nó trả về một bộ với tổng số thay thế và chuỗi mới thay vì chỉ chuỗi.