小编Mad*_*sen的帖子

从列表中过滤字符串

我正在尝试从列表中过滤字符串。任务是:

创建一个函数,该函数接受一个非负数和字符串列表,并返回一个没有字符串的新列表。

这是我的代码:

def filter_list(lst):
    intlist = []
    while len(lst) > 0:
        if isinstance(lst[0],int):
            if lst[0] >= 0
                intlist.append(lst[0])
                lst.remove(lst[0])
            else:
                lst.remove(lst[0])
        if isinstance(lst[0],str):
            lst.remove(lst[0])
    return (intlist)
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误,似乎我陷入了无限的 while 循环中,尽管我不明白是怎么回事,因为我逐渐从列表中删除了项目,当列表为空时循环结束。

python filter python-3.x

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

标签 统计

filter ×1

python ×1

python-3.x ×1