Home > Net >  How to alter this loop to insert the word "CHANGE"
How to alter this loop to insert the word "CHANGE"

Time:11-19

Here is the code I am working with:

words_input = "Duck Bear Bear Duck Duck Bear Bear Bear Bear Duck Bear Bear Duck Duck"

replacements = [{'Bear': ["Black", "Woods"], 'Duck': ["Bird", "Feathers"]},
                {'Bear': ["Grizzly", "Woods"], 'Duck': ["Quack", "Feathers"]},
                {'Bear': ["Grizzly", "Black"], 'Duck': ["Quack", "Bird"]}]

output = []
index_replace = 0
count_replace = {key: 0 for key in replacements[0].keys()}
for word in words_input.split():
    if count_replace[word] == len(replacements[index_replace][word]):
        # We need to cycle through the replacements
        index_replace = (index_replace   1) % len(replacements)
        count_replace = {key: 0 for key in replacements[0].keys()}
    idx_word = count_replace[word]
    output.append(replacements[index_replace][word][idx_word])
    count_replace[word]  = 1

output_words = ' '.join(output)
print(output_words)
# Bird Black Woods Feathers Quack Grizzly Woods Grizzly Black Quack Black Woods Bird Feathers

I would like the desired output to insert the word "CHANGE" everytime the dictionary list changes so that the output would look like

Bird Black Woods Feathers CHANGE Quack Grizzly Woods CHANGE Grizzly Black Quack CHANGE Black Woods Bird Feathers

instead of

Bird Black Woods Feathers Quack Grizzly Woods Grizzly Black Quack Black Woods Bird Feathers

CodePudding user response:

Just add output.append("CHANGE") when... the list changes?

words_input = "Duck Bear Bear Duck Duck Bear Bear Bear Bear Duck Bear Bear Duck Duck"

replacements = [{'Bear': ["Black", "Woods"], 'Duck': ["Bird", "Feathers"]},
                {'Bear': ["Grizzly", "Woods"], 'Duck': ["Quack", "Feathers"]},
                {'Bear': ["Grizzly", "Black"], 'Duck': ["Quack", "Bird"]}]

output = []
index_replace = 0
count_replace = {key: 0 for key in replacements[0].keys()}
for word in words_input.split():
    if count_replace[word] == len(replacements[index_replace][word]):
        output.append("CHANGE") # << here is the change
        # We need to cycle through the replacements
        index_replace = (index_replace   1) % len(replacements)
        count_replace = {key: 0 for key in replacements[0].keys()}
    idx_word = count_replace[word]
    output.append(replacements[index_replace][word][idx_word])
    count_replace[word]  = 1

output_words = ' '.join(output)
print(output_words)
#Bird Black Woods Feathers CHANGE Quack Grizzly Woods CHANGE Grizzly Black Quack CHANGE Black Woods Bird Feathers

CodePudding user response:


replacements = [{'Bear': ["Black", "Woods"], 'Duck': ["Bird", "Feathers"]},
                {'Bear': ["Grizzly", "Woods"], 'Duck': ["Quack", "Feathers"]},
                {'Bear': ["Grizzly", "Black"], 'Duck': ["Quack", "Bird"]}]

output = []
index_replace = 0
count_replace = {key: 0 for key in replacements[0].keys()}
for word in words_input.split():
    if count_replace[word] == len(replacements[index_replace][word]):
        # We need to cycle through the replacements
        index_replace = (index_replace   1) % len(replacements)
        --- add--- output.append("CHANGE")
        count_replace = {key: 0 for key in replacements[0].keys()}
    idx_word = count_replace[word]
    output.append(replacements[index_replace][word][idx_word])
    count_replace[word]  = 1

output_words = ' '.join(output)
print(output_words)
# Bird Black Woods Feathers Quack Grizzly Woods Grizzly Black Quack Black Woods Bird Feathers```
  • Related