所以给了我一个列表,我必须打印列表中每个项目的类型.我可以清楚地看到有字符串和整数,但我需要它在Python中打印出来.我们刚刚学习了循环,所以我觉得这就是他们正在寻找的东西,但我无法打印出来.
本质上,该type函数接受一个对象并返回它的类型.请尝试以下代码:
for item in [1,2,3, 'string', None]:
print type(item)
Run Code Online (Sandbox Code Playgroud)
输出:
<type 'int'>
<type 'int'>
<type 'int'>
<type 'str'>
<type 'NoneType'>
Run Code Online (Sandbox Code Playgroud)
Here is how I would do it using type().
myList = [1,1.0,"moo"] #init the array
for i in myList:
print(type(i)) #loop and print the type
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24254 次 |
| 最近记录: |