Python:如何在列表中打印类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35028272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: How to print types within a list
提问by RPmich
So I was given a list and I must print the type of each item in the list. I can clearly see that there are strings and integers but I need it to print out in Python. We just learned for loopsso I feel like that is what they are looking for but I cannot get it to print out.
所以我得到了一个列表,我必须打印列表中每个项目的类型。我可以清楚地看到有字符串和整数,但我需要它在 Python 中打印出来。我们刚刚学习了for 循环,所以我觉得这就是他们正在寻找的东西,但我无法将其打印出来。
回答by intboolstring
回答by wasp8898
use the type
built in function of python.
使用type
python的内置函数。
lst = ['string', 1, 2, 'another string']
for element in lst:
print type(element)
output:
输出:
<type 'str'>
<type 'int'>
<type 'int'>
<type 'str'>
回答by Rudrani Angira
回答by ZetaRift
foo = [1, 0.2, "bar"]
for i in foo:
print(type(i))
Should print out the type of each item
应该打印出每个项目的类型
回答by Ashish Kumar
ls = [type(item) for item in list_of_items]
print(ls)