Bạn có thể làm điều đó với một cuộc gọi đến sub
:
big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
the_message = big_regex.sub["repl-string", str[word[1]]]
Example:
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
Lưu ý rằng việc sử dụng str.replace
có thể dẫn đến các lỗi tinh tế:
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
Trong khi sử dụng
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
0 cho kết quả chính xác:>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
Như thg435 chỉ ra, nếu bạn muốn thay thế các từ chứ không phải mọi chuỗi con, bạn có thể thêm các ranh giới từ vào regex:
big_regex = re.compile[r'\b%s\b' % r'\b|\b'.join[map[re.escape, words]]]
Điều này sẽ thay thế
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
1 trong >>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
2 nhưng không phải trong >>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
3. Sử dụng phương thức dịch [] để thay thế nhiều ký tự khác nhau. Bạn có thể tạo bảng dịch được chỉ định trong dịch [] bởi str. Maketrans []. Chỉ định một từ điển có khóa là ký tự cũ và có giá trị là chuỗi mới trong str.
Làm thế nào để bạn thay thế các chữ cái trong một chuỗi trong Python?
Phương thức python forth [] được sử dụng để tìm và thay thế các ký tự trong một chuỗi. Nó yêu cầu một chuỗi con phải được thông qua như một đối số; Các chức năng tìm thấy và thay thế nó. Phương thức thay thế [] thường được sử dụng trong làm sạch dữ liệu.
Phương thức chuỗi
Thí dụ
print[x]
Thay thế từ "chuối":
txt = "Tôi thích chuối"
x = txt.replace ["chuối", "táo"]
Hãy tự mình thử » All occurrences of the specified phrase will be replaced, if nothing else is specified.
Định nghĩa và cách sử dụng
Phương thức
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
4 thay thế một cụm từ được chỉ định bằng một cụm từ được chỉ định khác.Lưu ý: Tất cả các lần xuất hiện của cụm từ được chỉ định sẽ được thay thế, nếu không có gì khác được chỉ định.
Giá trị tham số | Tham số |
Sự mô tả | OldValue |
Yêu cầu. Chuỗi để tìm kiếm | NewValue |
Yêu cầu. Chuỗi để thay thế giá trị cũ bằng
Làm thế nào để bạn thay thế các chữ cái trong một chuỗi trong Python?
Phương thức python forth [] được sử dụng để tìm và thay thế các ký tự trong một chuỗi. Nó yêu cầu một chuỗi con phải được thông qua như một đối số; Các chức năng tìm thấy và thay thế nó. Phương thức thay thế [] thường được sử dụng trong làm sạch dữ liệu.
Phương thức chuỗi
Thí dụ
print[x]
Thay thế từ "chuối":
Làm thế nào để bạn thay thế các chữ cái trong một chuỗi trong Python?
Phương thức python forth [] được sử dụng để tìm và thay thế các ký tự trong một chuỗi. Nó yêu cầu một chuỗi con phải được thông qua như một đối số; Các chức năng tìm thấy và thay thế nó. Phương thức thay thế [] thường được sử dụng trong làm sạch dữ liệu.
Phương thức chuỗi
Thí dụ
print[x]
Thay thế từ "chuối":
Sử dụng phương thức dịch [] để thay thế nhiều ký tự khác nhau. Bạn có thể tạo bảng dịch được chỉ định trong dịch [] bởi str. Maketrans []. Chỉ định một từ điển có khóa là ký tự cũ và có giá trị là chuỗi mới trong str.
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọcreplace[] in Python returns a copy of the string where all occurrences of a substring are replaced with another substring.
Bàn luận
Việc thay thế [] trong Python trả về một bản sao của chuỗi trong đó tất cả các lần xuất hiện của một chuỗi con được thay thế bằng một chuỗi con khác. & NBSP; string.replace[old, new, count]
Parameters:
- Cú pháp thay thế [] old substring you want to replace.
- Cú pháp: String.replace [cũ, mới, đếm] new substring which would replace the old substring.
- Cũ - con cũ bạn muốn thay thế. [Optional ] the number of times you want to replace the old substring with the new substring.
MỚI - Chất nền mới sẽ thay thế cho bộ nền cũ.It returns a copy of the string where all occurrences of a substring are replaced with another substring.
Đếm - [Tùy chọn] Số lần bạn muốn thay thế bộ con cũ bằng bộ nền mới. & NBSP;
Giá trị trả về: Nó trả về một bản sao của chuỗi trong đó tất cả các lần xuất hiện của một chuỗi con được thay thế bằng một chuỗi con khác. & NBSP;
Python3
Thay thế tất cả các phiên bản của một ký tự bằng cách sử dụng thay thế []
Trong ví dụ này, chúng tôi chỉ thay thế một ký tự duy nhất từ một chuỗi đã cho. Phương thức thay thế python [] là nhạy cảm trường hợp, và do đó nó thực hiện thay thế chuỗi con nhạy cảm trường hợp, tức là r trong không thay đổi.
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
5>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
6>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
5>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
8
5>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
6 >>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
7 >>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
grrks FOR grrks geeks FOR geeks
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
8>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
6 >>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
0>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
1>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
2223 >>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
4
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
Đầu ra: & nbsp;
Python3
Thay thế tất cả các phiên bản của chuỗi
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
5>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
6>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
5>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
5>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
6>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
2>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
8>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
9
5>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
6 >>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
7 >>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
8>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
6 >>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
0>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
1>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
2223 >>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
4
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
Đầu ra: & nbsp;count=3.
Python3
>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
5>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile['|'.join[map[re.escape, prohibitedWords]]]
>>> the_message = big_regex.sub["", 'this message contains Some really Random Words']
>>> the_message
'this message contains really '
6 big_regex = re.compile[r'\b%s\b' % r'\b|\b'.join[map[re.escape, words]]]
2>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
5>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
5>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
3>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
2big_regex = re.compile[r'\b%s\b' % r'\b|\b'.join[map[re.escape, words]]]
7>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
9>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
5>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
5grrks FOR grrks geeks FOR geeks1
>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
2big_regex = re.compile[r'\b%s\b' % r'\b|\b'.join[map[re.escape, words]]]
7>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
... text = text.replace[word, 'swords']
...
>>> text
'a sample message with sswords swords'
2grrks FOR grrks geeks FOR geeks5
>>> big_regex = re.compile['|'.join[map[re.escape, words]]]
>>> big_regex.sub["swords", 'a sample message with random words']
'a sample message with swords swords'
9Output:
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks