Tôi cần một giải pháp trong đó các chuỗi được thay thế có thể là một biểu thức thông thường, ví dụ để giúp bình thường hóa một văn bản dài bằng cách thay thế nhiều ký tự khoảng trắng bằng một ký tự duy nhất. Dựa trên một chuỗi câu trả lời từ những người khác, bao gồm Miniquark và MMJ, đây là những gì tôi đã nghĩ ra:
def multiple_replace[string, reps, re_flags = 0]:
""" Transforms string, replacing keys from re_str_dict with values.
reps: dictionary, or list of key-value pairs [to enforce ordering;
earlier items have higher priority].
Keys are used as regular expressions.
re_flags: interpretation of regular expressions, such as re.DOTALL
"""
if isinstance[reps, dict]:
reps = reps.items[]
pattern = re.compile["|".join["[?P%s]" % [i, re_str[0]]
for i, re_str in enumerate[reps]],
re_flags]
return pattern.sub[lambda x: reps[int[x.lastgroup[1:]]][1], string]
Nó hoạt động cho các ví dụ được đưa ra trong các câu trả lời khác, ví dụ:
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
Điều chính đối với tôi là bạn cũng có thể sử dụng các biểu thức thông thường, ví dụ, chỉ để thay thế toàn bộ từ hoặc để bình thường hóa không gian trắng:
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
Nếu bạn muốn sử dụng các phím từ điển làm chuỗi bình thường, bạn có thể thoát khỏi chúng trước khi gọi nhiều_replace bằng ví dụ: Chức năng này:
def escape_keys[d]:
""" transform dictionary d by applying re.escape to the keys """
return dict[[re.escape[k], v] for k, v in d.items[]]
>>> multiple_replace[s, escape_keys[re_str_dict]]
"I don't want to change this name:\n Philip II of Spain"
Chức năng sau đây có thể giúp tìm các biểu thức chính quy sai lầm trong số các khóa từ điển của bạn [vì thông báo lỗi từ nhiều_replace không nói lắm]:
def check_re_list[re_list]:
""" Checks if each regular expression in list is well-formed. """
for i, e in enumerate[re_list]:
try:
re.compile[e]
except [TypeError, re.error]:
print["Invalid regular expression string "
"at position {}: '{}'".format[i, e]]
>>> check_re_list[re_str_dict.keys[]]
Lưu ý rằng nó không chuỗi thay thế, thay vào đó thực hiện chúng đồng thời. Điều này làm cho nó hiệu quả hơn mà không hạn chế những gì nó có thể làm. Để bắt chước ảnh hưởng của chuỗi, bạn có thể chỉ cần thêm nhiều cặp thay thế chuỗi và đảm bảo thứ tự dự kiến của các cặp:
>>> multiple_replace["button", {"but": "mut", "mutton": "lamb"}]
'mutton'
>>> multiple_replace["button", [["button", "lamb"],
... ["but", "mut"], ["mutton", "lamb"]]]
'lamb'
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
Đọc
Bàn luậnreplace[]
Việc thay thế một nhân vật bằng một nhân vật khác là một vấn đề phổ biến mà mọi lập trình viên Python sẽ làm việc trong quá khứ. Nhưng đôi khi, chúng tôi yêu cầu một giải pháp một dòng đơn giản có thể thực hiện nhiệm 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. & NBSP;
Python3
Phương pháp 1: Thay thế nhiều ký tự bằng cách sử dụng thay thế lồng nhau [] & nbsp;
Vấn đề này có thể được giải quyết bằng phương pháp thay thế lồng nhau, trong nội bộ sẽ tạo ra một temp. biến để giữ trạng thái thay thế trung gian. & nbsp;
test_str
____10
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
1>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
2>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
3>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
4 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
5 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
6>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
7Output:
The original string is : abbabba The string after replacement of positions : baabaab
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
8>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
0 >>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
0>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
1>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
2223>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
4>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
5__22221____>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
4>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
3____22225____25____32translate[] + maketrans[]
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
2>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
3def escape_keys[d]:
""" transform dictionary d by applying re.escape to the keys """
return dict[[re.escape[k], v] for k, v in d.items[]]
>>> multiple_replace[s, escape_keys[re_str_dict]]
"I don't want to change this name:\n Philip II of Spain"
5 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
5 def escape_keys[d]:
""" transform dictionary d by applying re.escape to the keys """
return dict[[re.escape[k], v] for k, v in d.items[]]
>>> multiple_replace[s, escape_keys[re_str_dict]]
"I don't want to change this name:\n Philip II of Spain"
7Python3
Phương pháp 2: Thay thế nhiều ký tự bằng dịch [] + maketrans [] & nbsp;
Phương pháp 1: Thay thế nhiều ký tự bằng cách sử dụng thay thế lồng nhau [] & nbsp;
Vấn đề này có thể được giải quyết bằng phương pháp thay thế lồng nhau, trong nội bộ sẽ tạo ra một temp. biến để giữ trạng thái thay thế trung gian. & nbsp;
test_str
____10
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
1>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
2>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
3>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
4 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
5 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
6>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
7Output:
The original string is : abbabba The string after replacement of positions : baabaab
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
8>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
0 >>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
0>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
1>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
2223>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
4>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
5__22221____>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
4>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
3____22225____25____32
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
>>> s = "I don't want to change this name:\n Philip II of Spain"
>>> re_str_dict = {r'\bI\b': 'You', r'[\n\t ]+': ' '}
>>> multiple_replace[s, re_str_dict]
"You don't want to change this name: Philip II of Spain"
2>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
3>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
5 def escape_keys[d]:
""" transform dictionary d by applying re.escape to the keys """
return dict[[re.escape[k], v] for k, v in d.items[]]
>>> multiple_replace[s, escape_keys[re_str_dict]]
"I don't want to change this name:\n Philip II of Spain"
5 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
7def escape_keys[d]:
""" transform dictionary d by applying re.escape to the keys """
return dict[[re.escape[k], v] for k, v in d.items[]]
>>> multiple_replace[s, escape_keys[re_str_dict]]
"I don't want to change this name:\n Philip II of Spain"
Python3
Phương pháp 2: Thay thế nhiều ký tự bằng dịch [] + maketrans [] & nbsp;
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à một cách được khuyến nghị để giải quyết vấn đề cụ thể này. Chỉ hoạt động trong Python2. & NBSP;
Vấn đề này có thể được giải quyết bằng phương pháp thay thế lồng nhau, trong nội bộ sẽ tạo ra một temp. biến để giữ trạng thái thay thế trung gian. & nbsp;
test_str
____10
>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
1>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
2>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
3>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
4 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
5 >>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
6>>> multiple_replace["[condition1] and --condition2--",
... {"condition1": "", "condition2": "text"}]
'[] and --text--'
>>> multiple_replace['hello, world', {'hello' : 'goodbye', 'world' : 'earth'}]
'goodbye, earth'
>>> multiple_replace["Do you like cafe? No, I prefer tea.",
... {'cafe': 'tea', 'tea': 'cafe', 'like': 'prefer'}]
'Do you prefer tea? No, I prefer cafe.'
7Output:
The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba