从 python 元组中弹出/删除项目 [英] pop/remove items out of a python tuple
问题描述
我不确定我是否能说清楚,但会尝试.
I am not sure if I can make myself clear but will try.
我在 python 中有一个元组,我按如下方式进行(见下面的代码).在检查过程中,我维护了一个计数器(我们称之为n")和满足特定条件的弹出"项目.
I have a tuple in python which I go through as follows (see code below). While going through it, I maintain a counter (let's call it 'n') and 'pop' items that meet a certain condition.
当然,一旦我弹出第一个项目,编号就会出错,我怎样才能更优雅地做我想做的事,同时只删除某个元组的某些条目?
Now of course once I pop the first item, the numbering all goes wrong, how can I do what I want to do more elegantly while removing only certain entries of a tuple on the fly?
for x in tupleX:
n=0
if (condition):
tupleX.pop(n)
n=n+1
推荐答案
好的,我想出了一个粗略的方法.
ok I figured out a crude way of doing it.
当条件在列表中满足时,我将n"值存储在 for 循环中(我们称之为 delList)然后执行以下操作:
I store the "n" value in the for loop when condition is satisfied in a list (lets call it delList) then do the following:
for ii in sorted(delList, reverse=True):
tupleX.pop(ii)
也欢迎任何其他建议.
这篇关于从 python 元组中弹出/删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!