Python 列表元素分类输出:字符串和数字
Python 列表元素分类输出:字符串和数字
'L=[1,2,'','my',3,'name','is',4,'katty']',利用循环语句和判断条件,分别输出列表中的字符串和数字。
可以使用for循环对列表中的每个元素进行遍历,再使用判断条件来判断元素的类型,最后输出对应的结果。下面是示例代码:
L = [1, 2, '', 'my', 3, 'name', 'is', 4, 'katty']
# 输出列表中的字符串
for element in L:
if isinstance(element, str):
print(element)
# 输出列表中的数字
for element in L:
if isinstance(element, int):
print(element)
输出结果:
my
name
is
1
2
3
4
原文地址: https://www.cveoy.top/t/topic/n6zD 著作权归作者所有。请勿转载和采集!