打印每个列表元素以及其数据类型

这个列表的一个例子


inList = [1.1, 2017, 3+4j, 'superbowl', /4, 5/, [1,2,3,5,12],{"make":'BMW', "model":'X5'}]


基本上,我需要编写一个通过列表的程序并与其数据类型一起打印每个列表项。

新手B. python 并需要帮助开始。 谢
已邀请:

风见雨下

赞同来自:

你写了那个 "我需要编写一个程序,该程序重复列表并与其数据类型一起打印每个列表项". 只有他可以找到相应的材料,但没有任何特定的东西。"

你真正的问题是你没有学会使用 Google 搜索编程问题的答案。 关键是为子任务打破您的问题,并搜索解决每个问题的方法:

迭代列表

获取数据类型

打印项目和数据类型

我忘了

https://www.google.com/search% ... Blist
. 第一个结果是
https://learnpythonthehardway.org/book/ex32.html
的 Learn Python The Hard Way, 其中包括此代码:


the_count = [1, 2, 3, 4, 5]
# this first kind of for-loop goes through a list
for number in the_count:
print "This is count %d" % number


这是结果


This is count 1
This is count 2
This is count 3
This is count 4
This is count 5


现在我忘了

https://www.google.com/search% ... UTF-8
. 第一个结果是 Stack Overflow 问题,
https://coderoad.ru/402504/
. 以下是其中一个答案的相应片段:

使用
type



>>> type/one/
<type 'int'="">


所以,现在我们知道如何识别和如何获得类型。 我们看到了如何打印,但不是如何立即打印两件事。 让我们谷歌

https://www.google.com/search% ... print
python . 第二个结果是一个部分
https://docs.python.org/2/tuto ... .html
教科书 Python 2.7. 事实证明,有很多方法可以同时打印几件事,但页面的一个简单示例是。


&gt;&gt;&gt; print 'We are the {} who say "{}!"'.format/'knights', 'Ni'/
We are the knights who say "Ni!"


如此折叠所有这些,我们得到:


for item in inList: 
print '{} {}'.format/item, type/item//


哪个印刷品:


1.1 <type 'float'="">
2017 <type 'int'="">
/3+4j/ <type 'complex'="">
superbowl <type 'str'="">
/4, 5/ <type 'tuple'="">
[1, 2, 3, 5, 12] <type 'list'="">
{'make': 'BMW', 'model': 'X5'} <type 'dict'="">


</type></type></type></type></type></type></type></type>

奔跑吧少年

赞同来自:

这是一个非常简单的问题,你可以轻松回答,只是看着
https://docs.python.org/2/tuto ... .html
.


for element in inList:
print element, type/element/

卫东

赞同来自:

对您的问题的简短答案是:


print map/lambda x: /x, type/x/.__name__/, inList/


这里使用功能
map

, 这需要两个参数:

将应用的功能;

要迭代的数组。

此功能通过阵列的每个元素,并将此功能应用于它们中的每一个。 应用程序的结果放在返回此函数的新阵列中。

另外,这里你可以看到关键字
lambda

, 这决定了匿名函数。 他接受了
x

作为参数,然后返回包含此参数的对和其类型的一行。

要回复问题请先登录注册