Python 单词替换列表打开关键字
Python word replacement list switch on key word
有谁知道如何修改这个脚本,以便它为单词“rat”的每个实例切换字典
word_replacement = [{'dog': 'Bob', 'cat': 'Sally', 'bird': 'John', 'rat': 'Pat'},
{'dog': 'Brown', 'cat': 'White', 'bird': 'Black', 'rat': 'Grey'},
{'dog': 'Bark', 'cat': 'Meow', 'bird': 'Chirp', 'rat': 'Squeek'}]
with open("main.txt") as main:
words = main.read().split()
replaced = []
for y in words:
replacement = word_replacement.get(y, y)
replaced.append(replacement)
text = ' '.join(replaced)
print (text)
new_main = open("main.txt", 'w')
new_main.write(text)
new_main.close()
示例输入:
dog bird rat dog cat cat rat bird rat cat dog
期望的输出:
Bob John Pat Brown White White Grey Chirp Squeek Sally Bob
已经指出 word_replacement
是一个列表,因此您必须使用在满足 rat
时递增的索引来访问其元素:
word_replacement = [{'dog': 'Bob', 'cat': 'Sally', 'bird': 'John', 'rat': 'Pat'},
{'dog': 'Brown', 'cat': 'White', 'bird': 'Black', 'rat': 'Grey'},
{'dog': 'Bark', 'cat': 'Meow', 'bird': 'Chirp', 'rat': 'Squeek'}]
input_str = "dog bird rat dog cat cat rat bird rat cat dog"
words = input_str.split()
replaced = []
dic_list_idx = 0
list_len = len(word_replacement)
for w in words:
replacement = word_replacement[dic_list_idx % list_len].get(w, w)
replaced.append(replacement)
if w == "rat":
dic_list_idx += 1
text = ' '.join(replaced)
print (text)
new_main = open("main.txt", 'w')
new_main.write(text)
new_main.close()
dic_list_idx % list_len
允许您在到达列表末尾时从第一个词典开始。
输出:
Bob John Pat Brown White White Grey Chirp Squeek Sally Bob
注意:在您的示例中,键和值之间似乎存在一些混淆(不应该将 bird
替换为 John
吗?)
方法有很多种,但首先想到的有两种:
- 在循环中有一个计数器,当你到达 'rat' 时你会增加它,如果你到达终点则重置为零:
i = 0
for y in words.split():
replacement = word_replacement[i][y]
replaced.append(replacement)
if y == 'rat':
i += 1
if i == len(word_replacement):
i = 0
text = ' '.join(replaced)
print(text)
- 始终使用列表中的第一个字典,但每次出现单词时 'rat' 弹出第一个字典并将其推到后面 :D
for y in words.split():
replacement = word_replacement[0][y]
replaced.append(replacement)
if y == 'rat':
word_replacement.append(word_replacement.pop(0))
text = ' '.join(replaced)
有谁知道如何修改这个脚本,以便它为单词“rat”的每个实例切换字典
word_replacement = [{'dog': 'Bob', 'cat': 'Sally', 'bird': 'John', 'rat': 'Pat'},
{'dog': 'Brown', 'cat': 'White', 'bird': 'Black', 'rat': 'Grey'},
{'dog': 'Bark', 'cat': 'Meow', 'bird': 'Chirp', 'rat': 'Squeek'}]
with open("main.txt") as main:
words = main.read().split()
replaced = []
for y in words:
replacement = word_replacement.get(y, y)
replaced.append(replacement)
text = ' '.join(replaced)
print (text)
new_main = open("main.txt", 'w')
new_main.write(text)
new_main.close()
示例输入:
dog bird rat dog cat cat rat bird rat cat dog
期望的输出:
Bob John Pat Brown White White Grey Chirp Squeek Sally Bob
已经指出 word_replacement
是一个列表,因此您必须使用在满足 rat
时递增的索引来访问其元素:
word_replacement = [{'dog': 'Bob', 'cat': 'Sally', 'bird': 'John', 'rat': 'Pat'},
{'dog': 'Brown', 'cat': 'White', 'bird': 'Black', 'rat': 'Grey'},
{'dog': 'Bark', 'cat': 'Meow', 'bird': 'Chirp', 'rat': 'Squeek'}]
input_str = "dog bird rat dog cat cat rat bird rat cat dog"
words = input_str.split()
replaced = []
dic_list_idx = 0
list_len = len(word_replacement)
for w in words:
replacement = word_replacement[dic_list_idx % list_len].get(w, w)
replaced.append(replacement)
if w == "rat":
dic_list_idx += 1
text = ' '.join(replaced)
print (text)
new_main = open("main.txt", 'w')
new_main.write(text)
new_main.close()
dic_list_idx % list_len
允许您在到达列表末尾时从第一个词典开始。
输出:
Bob John Pat Brown White White Grey Chirp Squeek Sally Bob
注意:在您的示例中,键和值之间似乎存在一些混淆(不应该将 bird
替换为 John
吗?)
方法有很多种,但首先想到的有两种:
- 在循环中有一个计数器,当你到达 'rat' 时你会增加它,如果你到达终点则重置为零:
i = 0
for y in words.split():
replacement = word_replacement[i][y]
replaced.append(replacement)
if y == 'rat':
i += 1
if i == len(word_replacement):
i = 0
text = ' '.join(replaced)
print(text)
- 始终使用列表中的第一个字典,但每次出现单词时 'rat' 弹出第一个字典并将其推到后面 :D
for y in words.split():
replacement = word_replacement[0][y]
replaced.append(replacement)
if y == 'rat':
word_replacement.append(word_replacement.pop(0))
text = ' '.join(replaced)