简体   繁体   中英

python using a dictionary to replace characters in a list of strings

I am trying to make a code breaking game where the user submits symbol/letter pairs to a dictionary to crack a code, I then want the code to use the dictionary to replace each instance of a symbol with the paired letter.

I have the following bits of code:

words = imported list of coded words where each letter is replaced by a symbol. from a text file so i can change later
clues = dictionary of symbol and letter pairs that can be added to, removed from

I have tried the following but it failed with: TypeError: list indices must be integers, not str

def converter(words,clues):

    progression = words


    for words in progression:#cycles through each coded word in the list
        for key in clues: #for each symbol in the dictionary
            progression[words] = progression[words].replace(key, clues[key]) #replaces


    return progression

Any help that anyone could offer I would be very grateful.

Adam

progression is a list. To access content from it, you need to use the index value, which is an integer, and not a string, hence the error.

You probably want:

for i, j in enumerate(words):
    words[i] = clues.get(j)

What enumerate does is loops through the list of words, where i is the index value and j is the content. .get() is similar to dict['key'] , but if the key is not found it returns None instead of raising an error.

Then words[i] modifies the list with the index number of the word

Haidro explained it pretty well, but I thought I'd expand his code, and also address another issue.

First off, as Inbar Rose pointed out, your naming conventions bad. It makes code much more difficult to read, debug, and maintain. Pick concise descriptive names, and make sure to follow PEP-8 . Avoid re-using the same variable name for different things, especially within the same scope.

Now, to the code:

words = ['Super', 'Random', 'List']
clues = {'R': 'S', 'd': 'r', 'a': 'e', 'o': 'e', 'm': 't', 'n': 'c'}


def decrypter(words, clues):

    progression = words[:]

    for i, word in enumerate(progression):
        for key in clues:
            progression[i] = progression[i].replace(key, clues.get(key))

    return progression

This now replaces characters in the content of progression[i] instead of replacing progression[i] with the key from clues .

Also, changed progression = words to progression = words[:] in order to create a copy of the list to act on. You pass in a reference to words, and then assign that same reference to progression. As you manipulate progression , so to do you manipulate words , rendering progression useless to be using in this case.

Example using:

print words
print decrypter(words, clues)
print words

Output using progression = words :

['Super', 'Random', 'List']
['Super', 'Secret', 'List']
['Super', 'Secret', 'List']

Output using progression = words[:] :

['Super', 'Random', 'List']
['Super', 'Secret', 'List']
['Super', 'Random', 'List']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM