在for循环的帮助下跳过10

时间:2020-01-28 04:08:38

标签: julia

我想获取以下值1, 10, 20, 30, ...,但我不怎么做茱莉亚:

 for (count, x) in enumerate(["x1", "x1.y1", "x1.y1.xyz22", "x133001", "x133001.y1", "x133001.y1.xyz22"])
     println(count + 10 - 1)
 end

最好的方法是什么?

先谢谢您

更新

The below code failed to run:

julia> count = 1
1

julia> for x in ["x1", "x1.y1", "x1.y1.xyz22", "x133001", "x133001.y1", "x133001.y1.xyz22"]
           if count == 1
               count = 10
           else
               count += 10
           println(count)
       end


ERROR: syntax: incomplete: "for" at REPL[6]:1 requires end
Stacktrace:
 [1] top-level scope at REPL[5]:0

2 个答案:

答案 0 :(得分:3)

在这里是不确定的精确解之一。使用(count,x),您通常可以使用count,但是将x用于开始和结束情况:

[ ]

获取您:for (count, x) in enumerate( ["x1", "x1.y1", "x1.y1.xyz22", "x133001", "x133001.y1", "x133001.y1.xyz22"] ) if x == "x1" print(count, ", ") else print(10 * (count - 1), ", ") end if x == "x133001.y1.xyz22" println("...") end end

答案 1 :(得分:1)

对于无限流,可以使用<div class="container"> <div class="iframe"> <iframe width="560" height="315" src="https://www.youtube.com/embed/C0DPdy98e4c" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </div>包中提供的类似Python的生成器。

安装方式:

ResumableFunctions.jl

生成inifnite流生成器函数:

using Pkg
Pkg.add("ResumableFunctions")
using ResumableFunctions

您可以通过以下方式初始化实例:

@resumable function infstream(start = 0, first_val = 0, step=1)
  "It starts with start value steps by step. However, for first value    
   first_val is returned."
  c = start
  while 1==1
    @yield if (c==start) first_val else c end
    c = c + step
  end
end

对于您的问题,您可以像这样使用它:

mycount = infstream(0, 1, 10)
mycount() # 1
mycount() # 10
mycount() # 20
# ... etc ad infinitum

给出:

counter, result = infstream(0, 1, 10), []
for x in ["x1", "x1.y1", "x1.y1.xyz22", "x133001", "x133001.y1", "x133001.y1.xyz22"]
    push!(result, counter())
end

使用可恢复的julia> result 6-element Array{Any,1}: 1 10 20 30 40 50 函数,您可以为算术无限序列创建任何可考虑的计数器。