在Julia数组理解中杀死For循环 [英] Killing a For loop in Julia array comprehension

查看:96
本文介绍了在Julia数组理解中杀死For循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Julia中有以下代码行:

I have the following line of code in Julia:

X=[(i,i^2) for i in 1:100 if i^2%5==0]

基本上,如果i^25的余数为零,则从i=1 to 100返回元组(i,i^2)的列表.我想做的是,在数组理解中,如果i^2变得大于1000,请打破for循环.但是,如果我实现

Basically, it returns a list of tuples (i,i^2) from i=1 to 100 if the remainder of i^2 and 5 is zero. What I want to do is, in the array comprehension, break out of the for loop if i^2 becomes larger than 1000. However, if I implement

X=[(i,i^2) for i in 1:100 if i^2%5==0 else break end]

我收到错误消息:syntax: expected "]".

有什么方法可以轻松地打破数组内部的for循环吗?我尝试过在线查找,但没有任何反应.

Is there any way to easily break out of this for loop inside the array? I've tried looking online, but nothing came up.

推荐答案

我不这么认为.您总是可以

I don't think so. You could always just

tmp(i) = (j = i^2; j > 1000 ? false : j%5==0)
X=[(i,i^2) for i in 1:100 if tmp(i)]

这篇关于在Julia数组理解中杀死For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆