MST

星途 面试题库

面试题:Python复杂条件下if与循环结构应用

假设有一个列表,其中包含一些整数和字符串,例如[1, 'abc', 2, 'def', 3]。编写Python代码,使用if语句结合循环结构,过滤掉列表中的字符串,并对剩下的整数进行平方操作,最后返回操作后的新列表。
15.7万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
original_list = [1, 'abc', 2, 'def', 3]
new_list = []
for item in original_list:
    if isinstance(item, int):
        new_list.append(item ** 2)
print(new_list)