Python列表/词典

时间:2015-09-22 18:05:24

标签: python python-2.7

   Streets= [{"index": 1, "name": "east 13th", "location": ["pizza store", "shoe store"]}, 
    {"index": 2, "name": "east 14th", "location": ["pizza store", "flower store"]},
    {"index": 3, "name": "east 15th", "location": ["shoe store", "massage parlor"]},
    {"index": 4, "name": "east 16th", "location": "shoe store"}]

我如何在东14街的位置打印一些东西?我试过了print Streets.values(),但这似乎不起作用 - 当列表中只有一个数组而不是四个数组(看似)时,它可以正常工作。

另外,我想将需求映射到位置。因此,例如当用户“去”时,进入东15号的一家鞋店。街道,另一个清单:

需要= {饥饿,口渴,衣着,住所}

被服装减少了。并且,如果一个人在没有穿着“衣服”的情况下再次尝试进入鞋店。需要,程序将返回类似

的输出

"你看了看这些物品,发现你并不需要他们能提供的任何东西。"

在这里没有按照代码输入,我会写下以下内容:

    If the user tries to enter into the location
       Check what needs are mapped to that location
       If the needs which are mapped to that location are currently unmet
          The user enters in to that location and interacts with the 'shop keep'
       If the needs which are mapped to that location are currently met
          The user enters in to that location, looks around, and leaves.

   Go back to default state

那么我将如何映射“饥饿”'需要两家披萨店和#39;它位于东部第13'和比萨饼店'位于东边14号'

1 个答案:

答案 0 :(得分:0)

您有一个包含词典的列表。您可以循环列表并在每个列表中搜索所需的键和值

 for d in Streets:
        if d["name"] == "east 14th":
            print(d["location"])

输出

['披萨店','花店']