将字符串转换为字典指定值



我是python新手。我正在尝试将消息字符串转换为指定的字典值。例如,123的输出应该是abc。
my code:'

import string
message = "57669.843.5987945.5464219238.4857.94.297825.123.85427.29.5937.973.82.123.8.75898.94.2985925.89.59.59.9.74224.6844.384765"
parts = message.replace('.', ':').split(':')

l = dict(zip(string.ascii_lowercase, range(1,27)))

我尝试将列表转换为整数,但我找不到解决方案

import string
l = dict(zip(range(1,27),string.ascii_lowercase))  # here I have changed the order
print(l)
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
message = "57669.843.5987945.5464219238.4857.94.297825.123.85427.29.5937.973.82.123.8.75898.94.2985925.89.59.59.9.74224.6844.384765"
parts = message.replace('.', ':').split(':')
print(parts)
#['57669', '843', '5987945', '5464219238', '4857', '94', '297825', '123', '85427', '29', '5937', '973', '82', '123', '8', '75898', '94', '2985925', '89', '59', '59', '9', '74224', '6844', '384765']

k=[]
for x in parts:
for y in x:
k.append(l[int(y)])
''.join(k)
#output
'egffihdceihgideedfdbaibchdhegidbighbeabchedbgbieicgigchbabchgehihidbiheibehieieiigdbbdfhddchdgfe'

您还可以通过单个for循环实现您的确切消息转换:

message = "57669.843.5987945.5464219238.4857.94.297825.123.85427.29.5937.973.82.123.8.75898.94.2985925.89.59.59.9.74224.6844.384765"
import string
l = dict(zip(range(1,27),string.ascii_lowercase))
lst=[]
for x in message:
if x!='.':
lst.append(l[int(x)])
else:
lst.append(x)
print(lst)
'egffi.hdc.eihgide.edfdbaibch.dheg.id.bighbe.abc.hedbg.bi.eicg.igc.hb.abc.h.gehih.id.biheibe.hi.ei.ei.i.gdbbd.fhdd.chdgfe'

相关内容

  • 没有找到相关文章

最新更新