首页 > 其他分享 >TypeError: 'dict_keys' object is not subscriptable

TypeError: 'dict_keys' object is not subscriptable

时间:2023-06-02 13:24:27浏览次数:44  
标签:aa TypeError dict1 bb keys cc dict

 

001、python报错

>>> dict1 = {"aa":300, "bb":500, "cc":400, "dd":700}
>>> dict1
{'aa': 300, 'bb': 500, 'cc': 400, 'dd': 700}
>>> dict1.keys()
dict_keys(['aa', 'bb', 'cc', 'dd'])
>>> dict1.keys()[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object is not subscriptable

 

002、报错原因 :python3不支持该语法

 

003、解决方法

>>> dict1 = {"aa":300, "bb":500, "cc":400, "dd":700}
>>> dict1
{'aa': 300, 'bb': 500, 'cc': 400, 'dd': 700}
>>> dict1.keys()
dict_keys(['aa', 'bb', 'cc', 'dd'])
>>> dict1.keys()[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object is not subscriptable
>>> list(dict1.keys())[2]         ## 增加list函数
'cc'

 

标签:aa,TypeError,dict1,bb,keys,cc,dict
From: https://www.cnblogs.com/liujiaxin2018/p/17451502.html

相关文章

  • AttributeError: 'dict_values' object has no attribute 'index'
     001、python报错如下:>>>dict1={"aa":700,"bb":400,"cc":300,"dd":600}>>>dict1.values().index(300)Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>......
  • dict 协议——这玩意和redis差不多
    dict协议初体验多说无益,直接上一个用了dict协议的服务让你们来体验一下首先在你的电脑上安装一个telnet客户端Windows和Mac/Linux上应该都有对应的客户端安装好了以后用这个命令来登陆由于编码原因,有些非英文字符在某些系统上可能会乱码telnetdict.org2628之后如果连......
  • 【Azure K8S】演示修复因AKS密钥过期而导致创建服务不成功的问题(The provided client
    问题描述在AzureKubernetes服务中,创建一个InternalLoadBalancer服务,使用以下yaml内容:internallb.yamlapiVersion:v1kind:Servicemetadata:name:ilb-myappannotations:service.beta.kubernetes.io/azure-load-balancer-internal:"true"spec:type:LoadBala......
  • 【Azure K8S】演示修复因AKS密钥过期而导致创建服务不成功的问题(The provided client
    问题描述在AzureKubernetes服务中,创建一个InternalLoadBalancer服务,使用以下yaml内容:internallb.yamlapiVersion:v1kind:Servicemetadata:name:ilb-myappannotations:service.beta.kubernetes.io/azure-load-balancer-internal:"true"spec:type:L......
  • QA|selenium在send_keys时报错dict object has no attribute ''|UI自动化测试
    Q:selenium在send_keys时报错dictobjecthasnoattribute'send_keys',如下图 增加了print(type(e1))发现确实是字典类型,怪了,按道理e1的type应该是selenium.webdriver.remote.webelement.WebElement才对,怎么会变成dict字典呢?我网上查了,按照(115条消息)Python运行selenium时......
  • Erlang 对dict、maps、array的部分性能测试
    竖轴:时间(微秒)横轴(数据量)备注(maps与dict的key是{name,整数}与整数在速度上差别不大,array的key是正整数)结论数据量在32-10000用maps的各种操作速度更快(但内存稍多,引用官方描述,此处没测)数据量1万以上,如果键是正整数,array与maps性能相当(官方推荐用array)数据量接近32个的时候是map......
  • vue3中 TypeError: track(...) is not a function
    我这边遇到的是在使用到element-plus的一个<el-input>标签时就开始出现(TypeError:track(...)isnotafunction),我怀疑这是element-plus与vue的版本问题,查看element-plusgithub的Releases 发现支持的版本比我当前使用的版本3.3.3低很多,应该element-plus不支持了,那怎么办,只能......
  • python操作mysql数据pymysql-执行语句select查询返回值直接返回dict字典类型或者list
    一、返回tuple元组类型(默认)fetchall()将结果放在二维数组里面,每一行的结果在元组里面importpymysqldefexport(table_name):conn=pymysql.connect(host='118.24.3.40',user='jxz',password='123456',db......
  • OnlineDictionary 倒计时体会
     电子辞典终于要结项了,这几天没命写报告,怎么会有这么多报告要写?GOD!有印象的生活是张开眼就开始写报告,画图,写到睡觉。Why theywant somanykindsofreports?真是要人命!  这次的项目,说实话,学到了很多,团队合作,互相体谅,技术学习。这些都是不可言谈的经......
  • OnlineDictionary
        WhatshouldItalkaboutit?It'sjustasimplecompetition ,theyjustwanttoseewhatcanwedo,howgoodideascanwethinkup.........    IjustwanttoexpressmyownthoughtstoshowwhatamIthinkingabout.Ijustwanttocoopratewith......