可以使用 for 循环对列表中的每个元素进行遍历,再使用判断条件来判断元素的类型,最后输出对应的结果。下面是示例代码:

L = [1, 2, '', 'my', 3, 'name', 'is', 4, 'katty']

输出字符串

for item in L: if isinstance(item, str): print(item)

输出数字

for item in L: if isinstance(item, int): print(item)

输出结果为:

'my' 'name' 'is' 1 2 3 4

Python 列表元素类型判断与输出 - 字符串和数字分离

原文地址: https://www.cveoy.top/t/topic/n6zA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录