如何从python字典中返回特定信息?

时间:2019-11-26 19:17:48

标签: python python-3.x dictionary

我正在尝试从字典中提取“名称”:

fork_fig = {'categories': [{'alias': 'burgers', 'title': 'Burgers'},
  {'alias': 'sandwiches', 'title': 'Sandwiches'},
  {'alias': 'salad', 'title': 'Salad'}],
 'name': 'Fork & Fig',}

我可以返回“名称”的第一个字母:“ Fork&Fig”,但是我无法获得全名。这是我到目前为止的代码:

def restaurant_name():
    for row in fork_fig['name']:
        return row
restaurant_name()

1 个答案:

答案 0 :(得分:4)

您应该查看有关dictionaries的python文档。

def restaurant_name():
    for row in fork_fig['name']:
        # this loop will iterates on the letters of the name.
        return row # return the first letter and leave the function

要获取名称,您可以执行此操作。

def restaurant_name():
    return fork_fig['name']:
相关问题