Python nested list of integers not returning as expected

I am writing this function that checks if a list of lists is a real sudoku puzzle. When I check lists for real integers, I get unexpected results.

For instance:

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

Doesn’t print anything, and when I through:

for i in lst:
  for v in i:
    if v is not int:
      print False

Prints everything False? Not sure what is going on, especially with the type showing that they are whole.

+3
source share
4 answers

Instead of talking

if v is int:

Which asks if v is an actual int type

Let's say

if isinstance(v, int):

which says v is an instance int(or subclass)

Here is an example, first with an integer (or instance int)

>>> v = 17
>>> type(v)
<type 'int'>
>>> v is int
False
>>> isinstance(v, int)
True
>>> 

Next with type

>>> v = int
>>> type(v)
<type 'type'>
>>> v is int
True
>>> isinstance(v, int)
False
>>> 
+5
source

- , int. isintance.

type(v) == int, False, v int, ; , type(v) is int, is - ( ).

+2
v is not int

- , ,

not v is int

is ( ), , . :

>>> x = (1,2,3)
>>> y = (1,2,3)
>>> x == y
True
>>> x is y
False

, v is int, , False. not False True, if .

isinstance(v, int) .

+2

, isinstance, , - if type(v) == int.

+1

All Articles