标签:$1 typeerror col highlight nbsp als erro str 其他
isinstance(1, int) 判断是否是int型
isinstance(1.0, float) 判断是否是float型
isinstance(s, str) 判断是否是字符串型
isinstance(a, dict) 判断对象a是否为字典
题目:传入函数的字符串中,[数字]、[字?]、[空格] 以及 [其他]的个数,并返回结果
# haha="a b c_$123"
haha=[1,"12",‘a‘,[1,2,3],{"name":"aaa","pwd":"111"}]
s=0 # 字符串
l=0 # 列表
n=0 # 数字
d=0 # 字典
q=0 # 其他
def f_c(a):
for i in a:
print(type(i))
if isinstance(i, str):
print(‘是字符串‘)
global s
s+=1
elif isinstance(i, list):
print(‘是列表‘)
global l
l += 1
elif isinstance(i, int):
print(‘是数字‘)
global n
n+=1
elif isinstance(i,dict):
print(‘是字典‘)
global d
d += 1
else:
print(‘是其他‘)
global q
q+=1
f_c(haha)
print("字符串:%s,列表:%s个,数字:%s个,字典:%s个,其他:%s个."%(s,l,n,d,q))
python isinstance函数 判断元素是否是字符串、int型、float型
标签:$1 typeerror col highlight nbsp als erro str 其他
原文地址:https://www.cnblogs.com/c-x-m/p/9402425.html