检查字典值是否不是int,或者int(value)是否不是有效的正整数的最简洁/python方法

检查字典值是否不是int,或者int(value)是否不是有效的正整数的最简洁/python方法,python,Python,我正在处理凌乱的数据,我需要找到任何不是正整数的数据点,或者使用int(value) 下面是一些示例场景 vardict = { 'a' : None, 'b' : 3.4, 'c' : -1, 'd' : 10, 'e' : -5.7, 'f' : '7', 'g' : [9], 'h' : {7}, 'i' : 3j, 'j' : r'8', 'k' : True, 'l'

我正在处理凌乱的数据,我需要找到任何不是正整数的数据点,或者使用
int(value)

下面是一些示例场景

vardict = {
    'a' : None, 
    'b' : 3.4, 
    'c' : -1, 
    'd' : 10, 
    'e' : -5.7, 
    'f' : '7', 
    'g' : [9], 
    'h' : {7}, 
    'i' : 3j, 
    'j' : r'8', 
    'k' : True, 
    'l' : False,
    'm': '-19',
    'o': '3.5'
    }
下面是一些可以处理查找所有非正整数的代码

for letter in 'abcdefghijklmnopqrs':
    if type(vardict.get(letter)) is not int or vardict.get(letter) < 0 :
        print(letter, True)
    else:
        print(letter, False)
但我也希望它发现情况f也是假的,因为int(7)变成了一个正整数

这是理想的输出

a True
b True
c True
d False
e True
f False
g True
h True
i True
j True
k True
l True
m True
n True
o True
p True
q True
r True
s True
我试过的

for letter in 'abcdefghijklmno':
    if type(vardict.get(letter)) is not int or vardict.get(letter) < 0 :
        if type(int(vardict.get(letter))) is int and int(vardict.get(letter)) > 0 :
            print(False)
        else:
            print(True)
    else:
        print(False)
对于“abcdefghijklmno”中的字母:
如果类型(vardict.get(字母))不是int或vardict.get(字母)<0:
如果类型(int(vardict.get(letter)))为int且int(vardict.get(letter))>0:
打印(假)
其他:
打印(真)
其他:
打印(假)
但这给了我

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-7c5f07bf3339> in <module>()
      1 for letter in 'abcdefghijklmno':
      2     if type(vardict.get(letter)) is not int or vardict.get(letter) < 0 :
----> 3         if type(int(vardict.get(letter))) is int and int(vardict.get(letter)) > 0 :
      4             print(False)
      5         else:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
“abcdefghijklmno”中的字母为1:
2如果类型(vardict.get(字母))不是int或vardict.get(字母)<0:
---->3如果类型(int(vardict.get(letter)))为int且int(vardict.get(letter))>0:
4印刷品(假)
5其他:
TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“NoneType”
试试这个

def is_positive_int(obj):
    if type(obj) == bool:
        return False
    if type(obj) == str:
        if obj.isdecimal():
            return int(obj) > 0
    elif type(obj) == int:
        return obj > 0
    return False


for i in vardict:
    print(i, not is_positive_int(vardict[i]))
除了未加工的细绳外,其他的都在工作

a True
b True
c True
d False
e True
f False
g True
h True
i True
j False
k True
l True
m True
o True

type(int(x))是int
没有意义,
int
的返回值总是一个
int
对象。所以我想我需要检查int(x)是否可能。这很棘手,因为在我的情况下,我想过滤掉3.5,但int(3.5)会返回int。我不想过滤掉“3”,因为它可以通过int('3')转换成整数。
a True
b True
c True
d False
e True
f False
g True
h True
i True
j False
k True
l True
m True
o True