1.命令有什么问题,你将如何解决? [英] 1. What is the issue with the command, and how would you fix it?

查看:81
本文介绍了1.命令有什么问题,你将如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

response = {
    "instances": [
        {
            "id": "i-123456789abcdef10",
            "name": "instance1",
            "type": "t2.micro",
            "ami_id": "ami-ea45f78a",
        },
        {
            "id": "i-111213141516171819",
            "name": "instance2",
            "type": "t2.small",
            "ami_id": "ami-ea45f78a"
        },
        {
            "id": "i-1a1b1c1d1e1f202122",
            "name": "instance3",
            "type": "t2.micro",
            "ami_id": "ami-ea45f78a",
        },
        
        ]
}

set([i[ami_id]for i in response["instances"]])

What I have tried:

Traceback (most recent call last): File "/Users/oluwaseyibamigbade/Library/Containers/uk.co.insi
li.Run-Python/Data/5870.py", line 25, in <module>
   
 set([i[ami_id]for i in response["instances"]])
NameError: name 'ami_id' is not defined

推荐答案

我不是python专家,你的代码不是很清楚所以我假设 set([i [ami_id] for i in response [实例]])是导致错误的调用。



我认为你需要更改 i [ami_id] 我[self.ami_id] ,以便您访问实例成员。
I'm not python expert and your code isn't very clear so I am assuming set([i[ami_id]for i in response["instances"]]) is the call that is causing the error.

I think you need to change i[ami_id] to i[self.ami_id] in order for you to access instance members.


ami_id

不是声明的变量,它是你词典中的一个键,你只需要把它放到双引号中。然后执行。



例如你会尝试:



is not a declared variable, and it is a key in your dictionary, you just need to put it into double quotes. and then execute.

For example you would try with:

data = set([i["ami_id"]for i in response["instances"]])


你的集合理解需要<$ c的带引号的字符串$ c> ami_id 因此:

Your set comprehension needs a quoted string for ami_id thus:
set([i["ami_id"]for i in response["instances"]])


这篇关于1.命令有什么问题,你将如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆