我正在写这个函数,检查列表列表是否是一个有效的数独谜题.当我检查有效整数的列表时,我得到了意想不到的结果.
例如:
lst = [[1,2,3],[2,3,1],[4,2,1]]
for i in lst:
for v in i:
print type(v)
<type 'int'> #all the way through
for i in lst:
for v in i:
if v is int:
print True
Run Code Online (Sandbox Code Playgroud)
什么都不打印,当我进入时:
for i in lst:
for v in i:
if v is not int:
print False
Run Code Online (Sandbox Code Playgroud)
打印全部错误?不确定发生了什么,尤其是显示它们是整数的类型.
而不是说
if v is int:
Run Code Online (Sandbox Code Playgroud)
这是在询问v是否是int的实际类型
说
if isinstance(v, int):
Run Code Online (Sandbox Code Playgroud)
说v是实例化的int(或子类)
这是一个例子,首先是一个整数(或实例化int)
>>> v = 17
>>> type(v)
<type 'int'>
>>> v is int
False
>>> isinstance(v, int)
True
>>>
Run Code Online (Sandbox Code Playgroud)
接下来是一个类型
>>> v = int
>>> type(v)
<type 'type'>
>>> v is int
True
>>> isinstance(v, int)
False
>>>
Run Code Online (Sandbox Code Playgroud)