Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia 在向量上迭代时从向量中移除元素_Julia - Fatal编程技术网

Julia 在向量上迭代时从向量中移除元素

Julia 在向量上迭代时从向量中移除元素,julia,Julia,我的第一次尝试是: I = Vector{String}(["first", "second", "third", "fourth"]) for i in I if i == "fourth" splice!(I, 4) end print("$i\n") end 最终导致边界错误: BoundsError(String["first", "second", "third"], (5,)) 然后我用手写的方式算出了: I = Vector{Strin

我的第一次尝试是:

I = Vector{String}(["first", "second", "third", "fourth"])

for i in I
    if i == "fourth"
        splice!(I, 4)
    end
    print("$i\n")
end
最终导致边界错误:

BoundsError(String["first", "second", "third"], (5,))
然后我用手写的方式算出了:

I = Vector{String}(["first", "second", "third", "fourth"])

state = start(I)

while ! done(I, state)
    (i, state) = next(I, state)

    if i == "fourth"
        splice!(I, state - 1)
        print("Delete element i=$(state - 1)\n")
        state = state - 1
        continue
    end

    print("Got: i=$i state=$state\n")
end
输出:

Got: i=first state=2
Got: i=second state=3
Got: i=third state=4
Delete element i=4
但是,是的,这既不容易读,也不容易写。在对向量进行迭代时,有没有任何朱利安方法可以从向量中删除元素?或者是否有任何推荐的数据结构通过某些函数调用明确支持它?

解决方案1:使用shift!推

解决方案2:使用拼接

如果您真的不需要执行任何其他中间操作(例如打印),而只想标识和删除元素,那么这可能就是解决方法

进一步漫谈:

另外,关于您试图通过for循环实现这一点:在任何语言中使用for循环进行迭代时,一个基本规则是,您正在迭代的变量的状态不会改变。在最好的情况下,违反此规则通常会导致错误,在最坏的情况下,会导致未定义的行为和无声的错误。如果变量的状态是要改变的,那么您不是在看“for循环”迭代,而是看更一般的while循环,它不假设一致的状态。

也就是说,您在这里所做的是正确的方法,您不应该寻找涉及for循环的方法。如果你真的找到了一个,那就考虑这个坏代码,让它独立:P。但是,是的,有更好看的方法;但是值得注意的是,您所做的基本上是重新发现轮子,因为您在julia中明确了for循环实际依赖的接口。即以下代码:

本质上,将内部转换为其等效项:

解决方案1:使用shift!推

解决方案2:使用拼接

如果您真的不需要执行任何其他中间操作(例如打印),而只想标识和删除元素,那么这可能就是解决方法

进一步漫谈:

另外,关于您试图通过for循环实现这一点:在任何语言中使用for循环进行迭代时,一个基本规则是,您正在迭代的变量的状态不会改变。在最好的情况下,违反此规则通常会导致错误,在最坏的情况下,会导致未定义的行为和无声的错误。如果变量的状态是要改变的,那么您不是在看“for循环”迭代,而是看更一般的while循环,它不假设一致的状态。

也就是说,您在这里所做的是正确的方法,您不应该寻找涉及for循环的方法。如果你真的找到了一个,那就考虑这个坏代码,让它独立:P。但是,是的,有更好看的方法;但是值得注意的是,您所做的基本上是重新发现轮子,因为您在julia中明确了for循环实际依赖的接口。即以下代码:

本质上,将内部转换为其等效项:

呼叫接头!对于大型阵列,很多时候速度非常慢:

功能d!一、 i=1 而我@b时间t 6.752 s 2分配:7.63 MiB 899975元素数组{Int64,1}: ... 最好打电话给deleteat!仅使用一次迭代器,该迭代器生成应删除的所有索引,例如

函数d2!一、 删除!一、 如果I[I]==s,则I表示每个hindexi中的I 终止 功能t2 I=rand1:101000 s=1 d2!一、 我 终止 朱莉娅>@b时间t2 15.889 ms 8分配:7.63 MiB 900414元素数组{Int64,1}: ... 呼叫接头!对于大型阵列,很多时候速度非常慢:

功能d!一、 i=1 而我@b时间t 6.752 s 2分配:7.63 MiB 899975元素数组{Int64,1}: ... 最好打电话给deleteat!仅使用一次迭代器,该迭代器生成应删除的所有索引,例如

函数d2!一、 删除!一、 如果I[I]==s,则I表示每个hindexi中的I 终止 功能t2 I=rand1:101000 s=1 d2!一、 我 终止 朱莉娅>@b时间t2 15.889 ms 8分配:7.63 MiB 900414元素数组{Int64,1}: ...
我不知道你想要什么,但你真的想要!我接近你要找的东西?[是的,我想知道这是否与菠菜有关。]@rickhg12hs-lol,不幸的是,这不是我需要的。我需要能够从一个向量中删除任何元素,无论它在迭代时的位置如何。我不确定你想要什么,但它会弹出!我接近你要找的东西?[是的,我想知道这是否与菠菜有关。]@rickhg12hs-lol,不幸的是,这不是我需要的。我需要能够从向量中删除任何元素,不管它在迭代时的位置如何。Hanks,你的第二种方法正是我想要的。看起来很简单,不需要创建新数组,也不需要在迭代过程中简单地删除一个或多个元素。完美:谢谢,你的第二条路正是我想要的。看起来很简单,不会创建新数组,也不会简单地删除一个或多个元素 在对其进行迭代时使用ts。很 完美:
julia> I = Vector{String}(["first", "second", "third", "fourth", "fifth"]);
julia> Inew = Vector{String}(0);
julia> while !isempty(I)
         i = shift!(I);
         if i == "fourth"; println("Skipped $i"); 
         else println("Got: i = $i"); push!(Inew, i);
         end
       end
Got: i = first
Got: i = second
Got: i = third
Skipped fourth
Got: i = fifth

julia> show(Inew)
String["first", "second", "third", "fifth"]
julia> I = Vector{String}(["first", "second", "third", "fourth", "fifth"]);
julia> i = 1;
julia> while i <= length(I)
         if I[i] == "fourth"; splice!(I,i);
         else i += 1;
         end
       end
julia> show(I)
String["first", "second", "third", "fifth"]
julia> I = Vector{String}(["first", "second", "third", "fourth", "fifth"]);
julia> deleteat!(I, findin(I, ["second", "fourth"]))
3-element Array{String,1}:
 "first"
 "third"
 "fifth"
for i in MyCollection; print("$i "); end
state = start(MyCollection)
while !done( MyCollection, state)
  (i, state) = next(MyCollection, state)
  print("$i ")
end