从 python 元组中弹出/删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21682804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:23:05  来源:igfitidea点击:

pop/remove items out of a python tuple

pythontuples

提问by Steve Grafton

I am not sure if I can make myself clear but will try.

我不确定我是否可以让自己清楚,但会尝试。

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.

我在 python 中有一个元组,我按如下方式进行(见下面的代码)。在浏览它时,我维护了一个计数器(我们称之为“n”)和满足特定条件的“流行”项目。

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

采纳答案by Steve Grafton

ok I figured out a crude way of doing it.

好的,我想出了一个粗略的方法。

I store the "n" value in the for loop when condition is satisfied in a list (lets call it delList) then do the following:

当列表中满足条件时,我将“n”值存储在 for 循环中(我们称之为 delList),然后执行以下操作:

    for ii in sorted(delList, reverse=True):
    tupleX.pop(ii)

Any other suggestions are welcome too.

也欢迎任何其他建议。

回答by tk.

Maybe you want dictionaries?

也许你想要字典?

d = dict( (i,value) for i,value in enumerate(tple))
while d:
    bla bla bla
    del b[x]

回答by SingleNegationElimination

As DSMmentions, tuple's are immutable, but even for lists, a more elegant solution is to use filter:

如前所述DSMtuple's 是不可变的,但即使对于列表,更优雅的解决方案是使用filter

tupleX = filter(str.isdigit, tupleX)

or, if conditionis not a function, use a comprehension:

或者,如果condition不是函数,则使用推导式:

tupleX = [x for x in tupleX if x > 5]

if you really need tupleX to be a tuple, use a generator expression and pass that to tuple:

如果您确实需要 tupleX 作为元组,请使用生成器表达式并将其传递给tuple

tupleX = tuple(x for x in tupleX if condition)

回答by Reza K Ghazi

There is a simple but practical solution.

有一个简单但实​​用的解决方案。

As DSM said, tuples are immutable, but we know Lists are mutable. So if you change a tuple to a list, it will be mutable. Then you can delete the items by the condition, then after changing the type to a tuple again. That's it.

正如 DSM 所说,元组是不可变的,但我们知道列表是可变的。因此,如果将元组更改为列表,它将是可变的。然后您可以按条件删除项目,然后再次将类型更改为元组。就是这样。

Please look at the codes below:

请看下面的代码:

tuplex = list(tuplex)
for x in tuplex:
  if (condition):
     tuplex.pop(tuplex.index(x))
tuplex = tuple(tuplex)
print(tuplex)

For example, the following procedure will delete all even numbers from a given tuple.

例如,以下过程将从给定的元组中删除所有偶数。

tuplex = (1, 2, 3, 4, 5, 6, 7, 8, 9)
tuplex = list(tuplex)
for x in tuplex:
  if (x % 2 == 0):
     tuplex.pop(tuplex.index(x))
tuplex = tuple(tuplex)
print(tuplex)

if you test the type of the last tuplex, you will find it is a tuple.

如果你测试最后一个tuplex的类型,你会发现它是一个tuple。

Finally, if you want to define an index counter as you did (i.e., n), you should initialize it before the loop, not in the loop.

最后,如果你想像你所做的那样定义一个索引计数器(即 n),你应该在循环之前初始化它,而不是在循环中。

回答by Abdulvakaf K

Yes we can do it. First convert the tuple into an list, then delete the element in the list after that again convert back into tuple.

是的,我们可以做到。首先将元组转换为列表,然后删除列表中的元素,然后再次转换回元组。

Demo:

演示:

my_tuple = (10, 20, 30, 40, 50)

# converting the tuple to the list
my_list = list(my_tuple)
print my_list  # output: [10, 20, 30, 40, 50]

# Here i wanna delete second element "20"
my_list.pop(1) # output: [10, 30, 40, 50]
# As you aware that pop(1) indicates second position

# Here i wanna remove the element "50"
my_list.remove(50) # output: [10, 30, 40]

# again converting the my_list back to my_tuple
my_tuple = tuple(my_list)


print my_tuple # output: (10, 30, 40)

Thanks

谢谢

回答by ShadowPuppy

The best solution is the tuple applied to a list comprehension, but to extract one item this could work:

最好的解决方案是将元组应用于列表理解,但要提取一个项目,这可以工作:

def pop_tuple(tuple, n): return tuple[:n]+tuple[n+1:], tuple[n]

def pop_tuple(tuple, n): return tuple[:n]+tuple[n+1:], tuple[n]