[英]How to check whether a dictionary contains integer or string in python?
我是 python 的新手,我的字符串有问题我有一个像这样的字典{'roads': '20', 'rails': 'three'}
。 我想知道字典的值是int or str
。 我该怎么做?
input:
dict = {'roads': '20' , 'rails': 'three'}
Expected output:
dict = {'roads': 'int' , 'rails': 'object'}
您可以使用string.isdigit()
检查某事是否为数字
dictionary = {'roads': '20' , 'rails': 'three'}
output = {k: 'int' if v.isdigit() else 'object' for k,v in dictionary.items()}
{'roads': 'int', 'rails': 'object'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.