简体   繁体   English

为什么类型函数为字典返回 False?

[英]Why is the type function returning False for a dictionary?

After creating a dictionary, I run print(type([name]) is dict) and it says False.创建字典后,我运行 print(type([name]) is dict) 并显示 False。 It returns true for list.它为列表返回true。

I'm testing this in a simple file because I believe this is the source of error in a larger program.我正在一个简单的文件中对此进行测试,因为我相信这是较大程序中的错误来源。 In that program when I try to return the value by calling the key, it says I cannot call a key from a tuple, but I'm calling the key from a dictionary, not a tuple.在那个程序中,当我尝试通过调用键返回值时,它说我不能从元组调用键,但我从字典中调用键,而不是元组。 When I enter the type function like this it returns True for list , not tuple or dictionary.当我输入这样的类型函数时,它为list返回True ,而不是元组或字典。

dictionary = {'value1':(1,2,3), 'value2':(4,5,6)}

print(type([dictionary]) is dict)
print(type([dictionary]) is list)

I expect to see True for dict and False for list .我希望看到TruedictFalselist In actuality, I'm seeing False for dict and True for list .实际上,我看到dictFalselistTrue

Because type([dictionary]) is a list - the double brackets form a list containing the dict .因为type([dictionary])是一个list - 双括号形成一个包含dictlist

try type(dictionary) instead, it will be a dict尝试type(dictionary)相反,它将是一个dict

因为 type([dictionary]) 是一个列表。请尝试使用 type(dictionary) 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM