簡體   English   中英

在字典中找到列表的鍵時,如何替換其值? 蟒蛇

[英]How do i replace values of a list when found in the dictionary with its key? python

因此,我目前正在構建一個簡單的解碼器工具,它能夠根據從txt文件讀取的字典中存儲的值,將短信化的簡寫形式(如lol)解碼為完整短語。 我想做的就是逆轉這個過程。 我想從字符串中大聲笑出來,並用它的縮寫大聲笑代替。 我是python新手,所以我的知識目前受到很大限制。 我確實知道一個字符串是不可變的,所以我知道將字符串轉換為列表,但是我的問題是我如何分割字符串,同時仍然保持笑聲大聲,以便我可以免字典運行它。 這是我的代碼減去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()

我的問題是我如何拆分字符串,同時仍然保持笑聲大聲,以便我可以對字典進行操作

為什么要拆分字符串? 這是一個非常簡單的解決方案,我希望將說明無需拆分字符串即可解決此問題的另一種方法:

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工作原理-您將如何實現該功能?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM