简体   繁体   中英

Create replacement dictionary in Python

I have a secret code that I want to decode.

I've gotten a alpabeth not with letters, but with emojies. The first emoji is A, second is B, third is C etc:

🎅 = A
🤶 = B
❄ = C
⛄ = D

My entire emoji alpabeth is the following:

alphabet_emoji = "🎅🤶❄⛄🎄🎁🕯🌟✨🔥🥣🎶🎆👼🦌🛷"

Now I want to map each emoji to its letter using a dictionary. I tried the following code:

replacement_dictionary[emoji] = str(letter);

However this gives me error:

NameError: name 'replacement_dictionary' is not defined

My entire code:

# Input
alphabet_emoji = "🎅🤶❄⛄🎄🎁🕯🌟✨🔥🥣🎶🎆👼🦌🛷"
alphabet_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


# Connect emoji to alpabet
print("Length: " + str(len(alphabet_emoji)))
print("Emoji\tUnicode\tOccurrence\tLetter")
counter = Counter(alphabet_emoji)
x = 0
for emoji in alphabet_emoji:
    unicode = f'U+{ord(emoji):X}'
    occurrence = counter[emoji]
    letter = alphabet_uppercase[x]
    print(emoji + "\t" + unicode + "\t" + str(occurrence) + "\t" + letter)

    # Add to dictionary
    replacement_dictionary[emoji] = str(letter);


    # Counter
    x=x+1

CODE THAT SOLVED THE PROBLEM WITH Mr. Hobo COMMENT

# Input
alphabet_emoji = "🎅🤶❄⛄🎄🎁🕯🌟✨🔥🥣🎶🎆👼🦌🛷"
alphabet_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


# Connect emoji to alpabet
print("\nHexmax A")
print("Length: " + str(len(alphabet_emoji)))
print("Emoji\tUnicode\tOccurrence\tLetter")
counter = Counter(alphabet_emoji)
replacement_dictionary = dict()
x = 0
for emoji in alphabet_emoji:
    unicode = f'U+{ord(emoji):X}'
    occurrence = counter[emoji]
    letter = alphabet_uppercase[x]
    print(emoji + "\t" + unicode + "\t" + str(occurrence) + "\t" + letter)

    # Add to dictionary
    replacement_dictionary[emoji] = str(letter);

    x = x+1

You never/did not define variable replacement_dictionary , yet you access(ed) it. Maybe this is the right code...

# Input
alphabet_emoji = "🎅🤶❄⛄🎄🎁🕯🌟✨🔥🥣🎶🎆👼🦌🛷"
alphabet_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


# Connect emoji to alpabet
print("Length: " + str(len(alphabet_emoji)))
print("Emoji\tUnicode\tOccurrence\tLetter")

counter = Counter(alphabet_emoji)

# Fixed here
replacement_dictionary = {}

# Loop(s)
for emoji in alphabet_emoji:
    unicode = f'U+{ord(emoji):X}'
    occurrence = counter[emoji]
    letter = alphabet_uppercase[x]
    
    print(emoji + "\t" + unicode + "\t" + str(occurrence) + "\t" + letter)
    
    # Add to dictionary
    replacement_dictionary[emoji] = str(letter);

Or if you want to use ** instead of replacement_dictionary[...] , here is the code...

# Input
alphabet_emoji = "🎅🤶❄⛄🎄🎁🕯🌟✨🔥🥣🎶🎆👼🦌🛷"
alphabet_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


# Connect emoji to alpabet
print("Length: " + str(len(alphabet_emoji)))
print("Emoji\tUnicode\tOccurrence\tLetter")

counter = Counter(alphabet_emoji)

# Fixed here
replacement_dictionary = {}

# Loop(s)
for emoji in alphabet_emoji:
    unicode = f'U+{ord(emoji):X}'
    occurrence = counter[emoji]
    letter = alphabet_uppercase[x]
    
    print(emoji + "\t" + unicode + "\t" + str(occurrence) + "\t" + letter)
    
    # Add to dictionary
    replacement_dictionary = {emoji: str(letter), **replacement_dictionary};

First define the dictionary;

your_dict = {}

OR

your_dict = dict()

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