标签: remove-method

.NET - 从'foreach'循环中的List <T>中删除

我有我希望看起来像这样的代码:

List<Type> Os;

...

foreach (Type o in Os)
    if (o.cond)
        return;  // Quitting early is important for my case!
    else
        Os.Remove(o);

... // Other code
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为当您foreach在该列表上的循环内时,无法从列表中删除:

有没有一种解决问题的常用方法?

如果需要,我可以切换到不同的类型.

选项2:

List<Type> Os;

...

while (Os.Count != 0)
     if (Os[0].cond)
         return;
     else
         Os.RemoveAt(0);

... // Other code
Run Code Online (Sandbox Code Playgroud)

丑陋,但它应该工作.

.net foreach remove-method

46
推荐指数
8
解决办法
6万
查看次数

如何删除Ruby中字符串中的最后一个元音?

如何在字符串中定义-last元音?

例如,我有一个单词"经典"

我想找到"classs i c" 这个词的最后一个元音是字母" i ",并删除最后一个元音.

我在想 :

def vowel(str)
  result = ""
  new = str.split(" ")
  i = new.length - 1
  while i < new.length
    if new[i] == "aeiou"
      new[i].gsub(/aeiou/," ")
    elsif new[i] != "aeiou"
      i = -= 1
    end
  end
  return result
end
Run Code Online (Sandbox Code Playgroud)

ruby regex string remove-method

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

如何通过从用户输入中删除要删除的值来从列表(整数和字符串)中删除整数?

我在python中做一个菜单驱动的程序来插入和删除列表中的项目.我有一个包含整数和字符串的列表.我想删除整数.

所以我从用户那里得到了输入

list = [1, 2, 3, "hi", 5]
x = input("enter the value to be deleted")
# input is given as 2 
list.remove(x)
Run Code Online (Sandbox Code Playgroud)

但它给了我一个 ValueError

我将输入转换为int,它适用于整数,但不适用于字符串.

python list remove-method python-3.x

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

Python中的删除方法

我有以下代码:

tree = {'nodes':[1,2,3],'root':[1]}
nodes = tree['nodes']
nodes.remove(2)
print(tree['nodes'])
print(nodes)
Run Code Online (Sandbox Code Playgroud)

输出如下:

[1, 3]
[1, 3]
Run Code Online (Sandbox Code Playgroud)

我的问题可能很愚蠢,但我不明白为什么remove方法导致该tree变量也发生了变化?

我认为当我像nodes上面的例子一样创建一个新变量时,任何应用于这个变量的方法只会影响这个变量。

从这个例子中,我可以得出结论,它也对一个tree变量产生了影响。

它以某种方式与全局和局部变量有关吗?

python dictionary copy list remove-method

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

标签 统计

remove-method ×4

list ×2

python ×2

.net ×1

copy ×1

dictionary ×1

foreach ×1

python-3.x ×1

regex ×1

ruby ×1

string ×1