简体   繁体   中英

having trouble in python with syntax for .replace

I am getting a syntax error (line 8 rw.replace) when I try to import this line of code. It should work fine?


def mirror(word):
    mirrorletters = str([[p,q],[q,p],[d,b][b,d]])
    rw == reverse(word)
    while True:
        if item in word:
            for mirrorletters in rw:
                for p in rw:
                    print rw.replace('p','q')
                for q in rw:
                    print rw.replace('q','p')
                for d in rw:
                    print rw.replace('d','b')
                for b in rw:
                    print rw.replace('b','d')
        elif item not in word:
            print(rw)

I'm assuming what you are trying to do is reverse the string passed in, and replace "p" with "q" and vice versa and the same with "d" and "b"?

mirrorletters = str([[p,q],[q,p],[d,b][b,d]]) is raising an error because you are trying to reference p , q , d , and b , none of which exist. You want the string representation of those letters.

rw == reverse(word) is not setting rw to the contents of reverse(word) you are comparing with == .

Also, reverse() to my knowledge is not in the standard library for Python 2 or 3.

You can reverse a string by saying

word = 'hello' r = word[::-1] print(r) # prints "olleh"

this does what I believe you are trying to do.:

def mirror(word):

        # Make a list of characters to be swapped.
        # "d" will be swapped with "b" and vice versa.
        # The same goes for "q" and "p"
        mirror_letters = [('d', 'b'), ('q', 'p')]

        # Start with a new string
        new_word = ''

        # Go through each letter in the word and keep up with the index
        for letter in word:

            # Go through each "character set" in mirror_letters
            for char_set in mirror_letters:

                # enumerate the char_set and get an index and character of each item in the list
                for index, char in enumerate(char_set):

                    # If the letter is equal to the character, set letter to character
                    if letter == char:

                        # Doing a little bit of "reverse indexing" here to determine which character to set letter to
                        letter = char_set[::-1][index]

                        # If we set the letter, break so we don't possibly change back.
                        break

            # Add the letter to the new word
            new_word += letter

        return new_word[::-1]

Outputs:

print(mirror('abcd')) # prints "bcda"

print(mirror('pqrs')) # prints "srpq"

If you want ro mirror a string and then swap some letters, you could go for a function like this:

def mirrorString(string, swap=[]):
    mirroed = string[::-1]
    i = 0
    while chr(i) in mirroed:
        i += 1
    for a, b in swap:
        mirroed = mirroed.replace(a, chr(i)).replace(b, a).replace(chr(i), b)
    return mirroed

print(mirrorString("abcd", [('b', 'd'), ('p', 'q')])) # bcda

I'm first mirroing the string, then selecting a character that is not present in the string to use as a temporal placeholder, then im iterating over the swap pairs, and replacing the first letter of the pair by the placeholder, the second by the first and the placeholder by the second.

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