I am new to python, I have a problem with string I have a dictionary like this {'roads': '20', 'rails': 'three'}
. I would like to find whether the values of the dictionary is an int or str
. How do I do it?
input:
dict = {'roads': '20' , 'rails': 'three'}
Expected output:
dict = {'roads': 'int' , 'rails': 'object'}
You can use string.isdigit()
to check is something is number
dictionary = {'roads': '20' , 'rails': 'three'}
output = {k: 'int' if v.isdigit() else 'object' for k,v in dictionary.items()}
{'roads': 'int', 'rails': 'object'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.