Python:如何在列表中打印类型

RPm*_*ich 3 python types list

所以给了我一个列表,我必须打印列表中每个项目的类型.我可以清楚地看到有字符串和整数,但我需要它在Python中打印出来.我们刚刚学习了循环,所以我觉得这就是他们正在寻找的东西,但我无法打印出来.

Rud*_*ira 6

本质上,该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)


小智 5

ls = [type(item) for item in list_of_items]
print(ls)
Run Code Online (Sandbox Code Playgroud)


int*_*ing 2

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)