如何在字典中找到列表的值并用其键替换列表的值?蟒蛇

时间:2017-11-25 07:48:31

标签: python

所以我正在构建一个简单的解码器工具,它目前能够根据存储在从txt文件读入的字典中的值,将短信短语表达式(如lol)解码为其完整短语。我想做的是扭转这个过程。我想从字符串中大声笑出来,用它的缩写lol替换它。我是python的新手,所以我的知识目前有限。我知道一个字符串在不可变,所以我知道将字符串转换为列表,但我的问题是我如何拆分字符串仍然大声笑声,所以我可以运行它agaisnt字典。这是我的代码减去txt文件任何想法或评论将不胜感激。

class Decry:
    def __init__(self):
        self.dic_usr = 0
        self.decrypted = "none"
        self.encrypted = "none"
        self.f = 0
        self.list1 = []
        self.list2 = []
        self.list3 = []
        self.dict1 = []
        self.key1 = []
        self.key2 = []
        self.key3 = "none"

    def __buildDiction__(self):
        self.f = open("dict")
        self.build_list = self.f.read().splitlines()
        self.d_dict = {}
        for i in self.build_list:
            x = i.split(",")
            self.d_dict[x[0]] = x[1]
        return self.d_dict

    def decoder(self, usr):
        self.f = self.__buildDiction__()
        self.list1 = usr.split(" ")
        for i in self.list1:
            if i in self.f:
                self.list1[self.list1.index(i)] = self.f[i]
                self.decrypted = " ". join(self.list1)
        return self.decrypted

    def dictionary(self):
        self.f = self.__buildDiction__()
        self.list2 = []
        self.list3 = []
        self.dict1 = []
        for i in self.f:
            self.list3.append(i)
            self.list2.append(self.f[i])
        for n, g in zip(self.list3, self.list2):
            self.dict1.append(n)
            self.dict1.append(g)
            self.key1 = [self.dict1[i:i+2] for i in range(0, len(self.dict1), 2)]
            self.key2 = [" ".join(x) for x in self.key1]
            self.key3 = "\n".join(self.key2)
        return self.key3


def main():
    print("\nWelecome to quick decrypt!!!\n"
            " /~~~~~~~~~~~~~~~~~~~~~~/")
    print("\n\nUse the number you desire.\n"
          "Proceed at your own risk:\n"
          "  1. Decrypts\n"
          "  2. Read dictionary\n"
          "  3. Add definitions to dictionary\n"
          "  4. Quit")
    deco = Decry()
    option = int(input())
    if option == 1:
        usr_input = str(input("Enter phrase to be decoded:\n"))
        f = deco.decoder(usr_input)
        print(f, "\n")
        return main()

    if option == 2:
        f = deco.dictionary()
        print(f, "\n")
        return main()

    if option == 3:
        with open("dict", "a") as txt1:
            txt1.write("\n" + str(input("Enter code followed by definition with a comma no space:\n")))
        return main()



if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

  

我的问题是我如何拆分字符串并仍然大声笑出来,以便我可以对字典运行

为什么要拆分字符串?这是一个非常简单的解决方案,我希望能够说明另一种解决方法,而无需拆分字符串:

phrases = {'lol': 'laugh out loud', 'tbd': 'to be decided', 'btw': 'by the way'}
userString = "by the way here is a simple solution laugh out loud"
for abbr, phrase in phrases.items():
    userString = userString.replace(phrase, abbr)
print userString

产地:

btw here is a simple solution lol

对于较大的字符串,您可能需要考虑查看正则表达式或其他更有效的技术。

作为练习,您可能想要考虑string.replace的工作原理 - 您将如何实现该功能?