用Python创建替换字典



我有一个要解码的密码。

我得到的不是字母,而是表情符号。第一个表情符号是A,第二个是B,第三个是C等等:

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

我的整个表情符号alpabeth如下:

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

现在我想用字典把每个表情符号映射到它的字母。我尝试了以下代码:

replacement_dictionary[emoji] = str(letter);

然而,这给了我一个错误:

名称错误:名称"replacement_dictionary"未定义

我的整个代码:

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

# Connect emoji to alpabet
print("Length: " + str(len(alphabet_emoji)))
print("EmojitUnicodetOccurrencetLetter")
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

与Hobo先生一起解决问题的代码评论

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

# Connect emoji to alpabet
print("nHexmax A")
print("Length: " + str(len(alphabet_emoji)))
print("EmojitUnicodetOccurrencetLetter")
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

您从未/没有定义变量replacement_dictionary,但您访问了它。也许这是正确的代码。。。

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

# Connect emoji to alpabet
print("Length: " + str(len(alphabet_emoji)))
print("EmojitUnicodetOccurrencetLetter")
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[...],以下是代码。。。

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

# Connect emoji to alpabet
print("Length: " + str(len(alphabet_emoji)))
print("EmojitUnicodetOccurrencetLetter")
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};

首先定义字典;

your_dict = {}

your_dict = dict()

最新更新