Hướng dẫn python replace words in string from list - python thay thế các từ trong chuỗi từ danh sách

Bạn có thể làm điều đó với một cuộc gọi đến sub:

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

Cú phápString.replace (OldValue, NewValue, Count)
Giá trị tham sốTham số
Sự mô tảOldValue
Yêu cầu. Chuỗi để tìm kiếmNewValue

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
  • Bàn luận
  • 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

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

    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

    Đầ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

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

    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks

    Hướng dẫn python replace words in string from list - python thay thế các từ trong chuỗi từ danh sách

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

    Đầ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'
    
    2
    big_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'
    
    5
    grrks FOR grrks
    geeks FOR geeks
    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'
    
    2
    big_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'
    
    2
    grrks FOR grrks
    geeks FOR geeks
    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'
    
    9

    Output:    

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks

    Làm thế nào để bạn thay thế các từ trong một danh sách trong Python?

    Phương thức python String thay thế () Phương thức thay thế () 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.The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.

    Làm thế nào để bạn thay thế một mục trong một danh sách bằng các chuỗi trong Python?

    Nếu bạn muốn thay thế chuỗi các phần tử của danh sách, hãy sử dụng phương thức chuỗi thay thế () cho mỗi phần tử bằng cách hiểu danh sách.Nếu không có chuỗi được thay thế, áp dụng thay thế () sẽ không thay đổi nó, vì vậy bạn không cần chọn một phần tử có điều kiện nếu điều kiện.use the string method replace() for each element with the list comprehension. If there is no string to be replaced, applying replace() will not change it, so you don't need to select an element with if condition .

    Làm thế nào để bạn thay thế nhiều từ trong một chuỗi trong Python?

    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.. You can create the translation table specified in translate() by the str. maketrans() . Specify a dictionary whose key is the old character and whose value is the new string in the 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. is used to find and replace characters in a string. It requires a substring to be passed as an argument; the function finds and replaces it. The replace() method is commonly used in data cleaning.