Hướng dẫn how do you replace multiple elements in python? - làm thế nào để bạn thay thế nhiều phần tử trong python?

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:

Show
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<_%d>%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
  • Bàn luận
  • 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.'
    
    7

    Output:

    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.'
    
    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
    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.'
    
    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"
    
    7

    Python3

    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.'
    
    7

    Output:

    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.'
    
    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
    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.'
    
    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"
    
    7

    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.'
    
    7

    Output:

    The original string is : 34a7bb86abba032
    The string after replacement of positions : abbabba

    Làm thế nào để bạn thay thế nhiều yếu tố trong một danh sách trong Python?

    Một cách mà chúng ta có thể làm điều này là bằng cách sử dụng một vòng lặp. Một trong những thuộc tính chính của danh sách Python là chúng có thể chứa các giá trị trùng lặp. Vì điều này, chúng ta có thể lặp qua từng mục trong danh sách và kiểm tra giá trị của nó. Nếu giá trị là một giá trị chúng tôi muốn thay thế, thì chúng tôi thay thế nó.using a for loop. One of the key attributes of Python lists is that they can contain duplicate values. Because of this, we can loop over each item in the list and check its value. If the value is one we want to replace, then we replace it.

    Làm thế nào để bạn thay thế nhiều ký hiệu trong Python?

    Để thay thế nhiều ký tự trong một chuỗi:..
    Lưu trữ các ký tự được thay thế và thay thế trong từ điển ..
    Sử dụng str.Phương thức maketrans () để tạo bản đồ dịch ..
    Sử dụng str.dịch () phương thức để thay thế các ký tự ..

    Làm cách nào để thay thế nhiều mục trong một chuỗi?

    Ví dụ: Thay thế bằng cách sử dụng regex modulesub () và re.subn () để thay thế nhiều chuỗi con trong một chuỗi.sub () - Nó thay thế nội dung của một chuỗi dựa trên các mẫu.Nó lấy một mẫu hoặc một chuỗi làm đối số đầu tiên.using Regex module sub() and re. subn() to replace multiple substrings in a string. sub() - It replaces the contents of a string based on patterns. It takes a pattern or a string as the first argument.

    Làm thế nào để bạn thay thế tất cả các lần xuất hiện trong Python?

    Chuỗi Python |thay thế () 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.replace() The replace() in Python returns a copy of the string where all occurrences of a substring are replaced with another substring.