Python thay thế các mục trong danh sách bằng từ điển

Các khóa trong từ điển là các chuỗi con cần được thay thế và các giá trị tương ứng trong từ điển là các chuỗi thay thế. Giống như, trong trường hợp này,

  • "is" nên được thay thế bằng "AA"
  • “the” nên được thay thế bằng “BBB”
  • “và” nên được thay thế bằng “CCC”

Chuỗi cuối cùng sẽ giống như,

ThAA AA BBB last rain of Season CCC Jack AA here.

Có nhiều cách khác nhau để làm điều này. Hãy thảo luận từng cái một

Sử dụng str. chức năng thay thế []

Lớp chuỗi có hàm thành viên replace[to_be_replaced, replacement] và nó thay thế tất cả các lần xuất hiện của chuỗi con “to_be_replaced” bằng chuỗi “replacement”

Để thay thế tất cả nhiều từ trong một chuỗi dựa trên từ điển. Chúng ta có thể lặp lại tất cả các cặp khóa-giá trị trong từ điển và đối với mỗi cặp, thay thế tất cả các lần xuất hiện của chuỗi con “key” bằng chuỗi con “value” trong chuỗi gốc

Ví dụ

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]

đầu ra

ThAA AA BBB last rain of Season CCC Jack AA here.

Nó thay thế tất cả các khóa/từ từ điển trong một chuỗi bằng các giá trị tương ứng từ từ điển

Trên mỗi lần lặp lại, chúng tôi sử dụng phương thức str.replace[] để thay thế các chuỗi con trong chuỗi bằng các giá trị từ từ điển

các str. phương thức thay thế trả về một bản sao của chuỗi với tất cả các lần xuất hiện của chuỗi con được thay thế bằng thay thế được cung cấp

Phương thức nhận các tham số sau

TênMô tảcũChuỗi con mà chúng tôi muốn thay thế trong chuỗimớiSự thay thế cho mỗi lần xuất hiện của oldđếm Chỉ những lần xuất hiện đầu tiên của count được thay thế [tùy chọn]

Phương thức không thay đổi chuỗi gốc. Các chuỗi là bất biến trong Python

Sử dụng phương pháp str.lower[] nếu bạn cần chuyển đổi các khóa và giá trị của từ điển thành chữ thường

các str. phương thức Lower trả về một bản sao của chuỗi với tất cả các ký tự có vỏ được chuyển đổi thành chữ thường

Bạn cũng có thể sử dụng phương pháp re.sub[] để thay thế các từ trong chuỗi bằng cách sử dụng các mục của từ điển theo cách không phân biệt chữ hoa chữ thường

Ở đó. phương thức phụ trả về một chuỗi mới thu được bằng cách thay thế các lần xuất hiện của mẫu bằng thay thế được cung cấp

Sử dụng phương pháp dict.update[] để thay thế các giá trị trong từ điển, e. g. my_dict.update[{'key': 'new value'}]. Phương thức dict.update[] cập nhật từ điển với các cặp khóa-giá trị từ giá trị được cung cấp

Chúng tôi đã sử dụng phương pháp

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
1 để thay thế các giá trị trong từ điển

mệnh lệnh. phương thức cập nhật cập nhật từ điển với các cặp khóa-giá trị từ giá trị được cung cấp

Phương thức ghi đè các khóa hiện có của từ điển và trả về

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
2

Phương thức dict.update[] có thể được gọi với một từ điển khác hoặc có thể lặp lại các cặp khóa-giá trị [e. g. một danh sách các bộ có 2 phần tử mỗi bộ]

Bạn cũng có thể truyền đối số từ khóa cho phương thức dict.update[]

Ngoài ra, bạn có thể sử dụng toán tử giải nén từ điển

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
5

Thay thế các giá trị trong từ điển bằng giải nén từ điển #

Để thay thế các giá trị trong từ điển

  1. Sử dụng toán tử giải nén từ điển để giải nén các cặp khóa-giá trị vào một từ điển mới
  2. Chỉ định các khóa có giá trị được cập nhật
  3. Các giá trị mới sẽ ghi đè các giá trị của các khóa hiện có

Chúng tôi đã sử dụng toán tử giải nén từ điển

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
5 để giải nén các cặp khóa-giá trị của từ điển thành một từ điển mới

Đưa ra một danh sách từ điển, nhiệm vụ là viết chương trình Python để thay thế giá trị của một khóa cụ thể bằng chỉ số giá trị thứ k nếu giá trị của khóa là danh sách.  

ví dụ

Đầu vào. test_list = [{‘gfg’. [5, 7, 9, 1], 'là'. 8, 'tốt'. 10}, {'gfg'. 1, 'cho'. 10, 'chuyên viên máy tính'. 9}, {'tình yêu'. 3, 'gfg'. [7, 3, 9, 1]}], K = 2, key = “gfg” 
Đầu ra. [{'gfg'. 9, 'là'. 8, 'tốt'. 10}, {'gfg'. 1, 'cho'. 10, 'chuyên viên máy tính'. 9}, {'tình yêu'. 3, 'gfg'. 9}] 
Giải thích. gfg được gán với 9 là chỉ mục thứ 2 trong danh sách.
 

Đầu vào. test_list = [{‘gfg’. [5, 7, 9, 1], 'là'. 8, 'tốt'. 10}, {'gfg'. 1, 'cho'. 10, 'chuyên viên máy tính'. 9}], K = 2, key = “gfg” 
Đầu ra. [{'gfg'. 9, 'là'. 8, 'tốt'. 10}, {'gfg'. 1, 'cho'. 10, 'chuyên viên máy tính'. 9}] 
Giải thích. gfg được gán với 9 là chỉ mục thứ 2 trong danh sách.

Phương pháp số 1. Sử dụng vòng lặp + isinstance[]

Trong phần này, chúng tôi đã sử dụng isinstance[] để kiểm tra loại giá trị danh sách và vòng lặp được sử dụng để lặp qua từ điển

Python3




strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
7

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
8

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
9

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
1

ThAA AA BBB last rain of Season CCC Jack AA here.
2
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
4
ThAA AA BBB last rain of Season CCC Jack AA here.
5
ThAA AA BBB last rain of Season CCC Jack AA here.
6
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
80
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81______182
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
84
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
86
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
87
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
88
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
90
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
92
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
94
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
95

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
96
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
97_______25
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
86
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
ThAA AA BBB last rain of Season CCC Jack AA here.
02
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
94
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
ThAA AA BBB last rain of Season CCC Jack AA here.
06
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
84
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
95

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
96
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
97_______212
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
ThAA AA BBB last rain of Season CCC Jack AA here.
14
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81______25
ThAA AA BBB last rain of Season CCC Jack AA here.
6
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
82
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
ThAA AA BBB last rain of Season CCC Jack AA here.
14
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
84
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
86
ThAA AA BBB last rain of Season CCC Jack AA here.
25

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
27

ThAA AA BBB last rain of Season CCC Jack AA here.
28
ThAA AA BBB last rain of Season CCC Jack AA here.
29____230
ThAA AA BBB last rain of Season CCC Jack AA here.
31
ThAA AA BBB last rain of Season CCC Jack AA here.
32
ThAA AA BBB last rain of Season CCC Jack AA here.
33

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
35

ThAA AA BBB last rain of Season CCC Jack AA here.
36
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
38

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
40

ThAA AA BBB last rain of Season CCC Jack AA here.
41
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
43

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
45
ThAA AA BBB last rain of Season CCC Jack AA here.
46____247
ThAA AA BBB last rain of Season CCC Jack AA here.
48

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
50
ThAA AA BBB last rain of Season CCC Jack AA here.
51

ThAA AA BBB last rain of Season CCC Jack AA here.
50
ThAA AA BBB last rain of Season CCC Jack AA here.
53
ThAA AA BBB last rain of Season CCC Jack AA here.
54
ThAA AA BBB last rain of Season CCC Jack AA here.
55
ThAA AA BBB last rain of Season CCC Jack AA here.
56
ThAA AA BBB last rain of Season CCC Jack AA here.
57

ThAA AA BBB last rain of Season CCC Jack AA here.
58
ThAA AA BBB last rain of Season CCC Jack AA here.
59____23
ThAA AA BBB last rain of Season CCC Jack AA here.
61

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
63

ThAA AA BBB last rain of Season CCC Jack AA here.
28
ThAA AA BBB last rain of Season CCC Jack AA here.
29
ThAA AA BBB last rain of Season CCC Jack AA here.
66
ThAA AA BBB last rain of Season CCC Jack AA here.
31
ThAA AA BBB last rain of Season CCC Jack AA here.
32
ThAA AA BBB last rain of Season CCC Jack AA here.
33

đầu ra

Danh sách ban đầu là. [{'gfg'. [5, 7, 9, 1], 'là'. 8, 'tốt'. 10}, {'gfg'. 1, 'cho'. 10, 'chuyên viên máy tính'. 9}, {'tình yêu'. 3, 'gfg'. [7, 3, 9, 1]}]
Từ điển sửa đổi. [{'gfg'. 9, 'là'. 8, 'tốt'. 10}, {'gfg'. 1, 'cho'. 10, 'chuyên viên máy tính'. 9}, {'tình yêu'. 3, 'gfg'. 9}]

Phương pháp #2. Sử dụng khả năng hiểu từ điển + isinstance[]

Trong phần này, chúng tôi xây dựng lại từ điển với các giá trị từ điển đã sửa đổi bằng cách sử dụng isinstance[] và khả năng hiểu từ điển được sử dụng để tạo từ điển trung gian

Python3




strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
7

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
8

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
802

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
1

ThAA AA BBB last rain of Season CCC Jack AA here.
2
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
4
ThAA AA BBB last rain of Season CCC Jack AA here.
5
ThAA AA BBB last rain of Season CCC Jack AA here.
6
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
80
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81______182
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
84
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
86
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
87
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
88
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
90
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
92
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
94
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
95

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
96
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
97_______25
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
86
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
ThAA AA BBB last rain of Season CCC Jack AA here.
02
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
94
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
ThAA AA BBB last rain of Season CCC Jack AA here.
06
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
84
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
95

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
96
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
97_______212
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
89
ThAA AA BBB last rain of Season CCC Jack AA here.
14
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81______25
ThAA AA BBB last rain of Season CCC Jack AA here.
6
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
82
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
ThAA AA BBB last rain of Season CCC Jack AA here.
14
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
84
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
81
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
86
ThAA AA BBB last rain of Season CCC Jack AA here.
25

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
27

ThAA AA BBB last rain of Season CCC Jack AA here.
28
ThAA AA BBB last rain of Season CCC Jack AA here.
29____230
ThAA AA BBB last rain of Season CCC Jack AA here.
31
ThAA AA BBB last rain of Season CCC Jack AA here.
32
ThAA AA BBB last rain of Season CCC Jack AA here.
33

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
35

ThAA AA BBB last rain of Season CCC Jack AA here.
36
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
38

ThAA AA BBB last rain of Season CCC Jack AA here.
0

ThAA AA BBB last rain of Season CCC Jack AA here.
40

ThAA AA BBB last rain of Season CCC Jack AA here.
41
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
43

ThAA AA BBB last rain of Season CCC Jack AA here.
0

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
875

strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
876
ThAA AA BBB last rain of Season CCC Jack AA here.
3
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
878
ThAA AA BBB last rain of Season CCC Jack AA here.
53
ThAA AA BBB last rain of Season CCC Jack AA here.
54
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
881
ThAA AA BBB last rain of Season CCC Jack AA here.
56
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
883
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
884
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
885
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
3
ThAA AA BBB last rain of Season CCC Jack AA here.
41
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
889
strValue = "This is the last rain of Season and Jack is here."

# Dictionary containing mapping of 
# values to be replaced and replacement values
dictOfStrings = {'is' : 'AA',
                 'the': 'BBB',
                 'and': 'CCC'}

# Iterate over all key-value pairs in dict and 
# replace each key by the value in the string
for word, replacement in dictOfStrings.items[]:
    strValue = strValue.replace[word, replacement]

print[strValue]
890

Chủ Đề