Skip to main content
 首页 » 编程设计

python之如果小于 x,则弹出值

2025年05月04日10mq0036

我有这个字典数组

for row in array: 
        if row['val'] < 11: 
            array.pop(array.index(row)) 

如果其中一个值低于某个阈值,我将尝试从数组中删除字典。它有效,但仅适用于数组中的一项

我现在的解决方案是运行 for 语句两次,然后删除多余的值。我该怎么办?

请您参考如下方法:

你 shouldn't modify a collection that you're iterating over .相反,使用 list comprehension :

array = [row for row in array if row['val'] >= 11] 

另外,让我们弄清楚另一件事。 python doesn't have native arrays .它有列表。