首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python列表/字典

Python列表/字典
EN

Stack Overflow用户
提问于 2015-09-23 02:05:24
回答 1查看 70关注 0票数 0
代码语言:javascript
复制
   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街的一家鞋店时,另一个列表是:

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

减少了“衣物”。而且,如果一个人在没有“衣服”需要的情况下试图再次进入鞋店,程序将返回如下输出

“你看着这些东西,意识到你不需要他们能提供的任何东西。”

这里不是以代码的形式键入它,我会像下面这样写一些东西:

代码语言:javascript
复制
    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

那么,我如何将“饥饿”需求映射到位于“东13号”的“披萨店”和位于“东14号”的“披萨店”呢?

EN

回答 1

Stack Overflow用户

发布于 2015-09-23 02:40:38

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

代码语言:javascript
复制
 for d in Streets:
        if d["name"] == "east 14th":
            print(d["location"])

输出

‘披萨店’,‘花店’

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32723850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档