小编gee*_*sam的帖子

操作列表 - 将整数提取到新列表

我有一个由字符串和整数组成的列表.我必须只弹出整数并将其放在一个单独的列表中.我的代码:

list1=['a','b','c','d','e','f','g','h',1,2,3]
list=[]
x=0
for i in list1:
     if isinstance(i,int) :
        list.append(i)
        list1.pop(x)
     x += 1

print(list1)
print(list)
Run Code Online (Sandbox Code Playgroud)

输出上述代码

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 2]
[1, 3]
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么我的代码没有删除所有整数?我的代码出了什么问题?

python integer

1
推荐指数
1
解决办法
63
查看次数

标签 统计

integer ×1

python ×1