通过字典正确解析多个键

2024-10-02 10:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了这个函数来解析我创建的缩写和含义词典。但它对较长的缩写词不起作用。我认为这是找到第一个片段并把它吐出来。我希望它获取整个输入并返回响应该输入的值。单字母和双字母基本上都有效,但较长的字母是不行的

我的密钥示例:值 {'b00n':['new person'],'hv':['have'],'wuwtb':['you want to talk'],'l8rz':['later'],'jhm':['just hold me']

def main():
    game = True
    myDict = CreateDictionary('textToEnglish.csv')
    print(myDict)
    while game == True:
        abbrev = input("Please enter text abbreviations seperated by comma:")
        newList = list(abbrev)
        print([v for k, v in myDict.items() if k in newList])

        answer = input("Would you like to input more abbreviations? yes(y) or no(n):")
        if answer == "y":
            game = True
        else:
            game = False

Tags: to函数answerinyougametrueinput
1条回答
网友
1楼 · 发布于 2024-10-02 10:28:13

abbrev是一个字符串,当您将其转换为列表时,您将得到每个字母的列表:

>>> abbrev = 'one, two, three'
>>> list(abbrev)
['o', 'n', 'e', ',', ' ', 't', 'w', 'o', ',', ' ', 't', 'h', 'r', 'e', 'e']

您可能需要这样的内容:

>>> abbrev.split(',')
['one', ' two', ' three']

相关问题 更多 >

    热门问题