如何在Python中使用迭代和嵌套for循环获取列表列表中每个元素的数据类型?

问题描述 投票:0回答:1

有一个列表由三个包含不同数据类型的单独列表组成。我想使用迭代和嵌套 for 循环检索每个列表中每个项目的数据类型。

x_list
是列表列表的名称

for n in range (len(x_list[0])):
    for m in (x_list):
        print (type(m))

返回的所有项目均带有“列表”类。很确定这没有正确迭代每个项目。

python
1个回答
0
投票
#Let us assume the following list
ls = [[5, 'a', 5.04],[True, 'b', 4]]

for x in ls:
    for y in x:

        #For the sake of understanding, printing both element and type
        print(y, type(y))

'''Output:
5 <class 'int'>
a <class 'str'>
5.04 <class 'float'>
True <class 'bool'>
b <class 'str'>
4 <class 'int'>
'''
© www.soinside.com 2019 - 2024. All rights reserved.