Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过字典正确解析多个键_Python_Parsing_Dictionary - Fatal编程技术网

Python 通过字典正确解析多个键

Python 通过字典正确解析多个键,python,parsing,dictionary,Python,Parsing,Dictionary,我已经编写了这个函数来解析我创建的缩写和含义词典。但它对较长的缩写词不起作用。我认为这是找到第一个片段并把它吐出来。我希望它获取整个输入并返回响应该输入的值。单字母和双字母基本上都有效,但较长的字母是不行的 我的密钥示例:值 {'b00n':['new person'],'hv':['have'],'wuwtb':['you want to talk'],'l8rz':['later'],'jhm':['just hold me'] def main(): game = True

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

我的密钥示例:值 {'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

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

>>> abbrev = 'one, two, three'
>>> list(abbrev)
['o', 'n', 'e', ',', ' ', 't', 'w', 'o', ',', ' ', 't', 'h', 'r', 'e', 'e']
您可能需要这样的内容:

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

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

>>> abbrev = 'one, two, three'
>>> list(abbrev)
['o', 'n', 'e', ',', ' ', 't', 'w', 'o', ',', ' ', 't', 'h', 'r', 'e', 'e']
您可能需要这样的内容:

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

现在听起来很明显!非常感谢。现在听起来很明显!非常感谢。顺便说一句,你应该总是将布尔值与“is”进行比较,而不是“while game==True”使用“while game is True”。作为一个单独的风格注释,我建议使用“while game”而不是“while game is True”,因为第一个更简洁(虽然你可能会说第二个更容易阅读)。作为旁白,你应该总是将布尔值与“is”进行比较,因此在游戏为真时使用“while game==True”,而不是“while game==True”。作为一个单独的风格注释,我建议使用“while game”而不是“while game is True”,因为第一个更简洁(尽管你可能会说第二个更具可读性)。