arrays - 在 Julia 中构建遍历多个索引的数组

标签 arrays julia indices

我想用更优雅的语法得到以下结果

julia> collect(Iterators.flatten([[(x,y) for y in 1:x] for x in 1:3]))
6-element Array{Tuple{Int64,Int64},1}:
 (1, 1)
 (2, 1)
 (2, 2)
 (3, 1)
 (3, 2)
 (3, 3)

我尝试了类似 [(x,y) for y in 1:x, x in 1:3] 但我得到 ERROR: UndefVarError: x not defined .

最佳答案

只需像这样颠倒变量的顺序:

julia> [(x,y) for x in 1:3 for y in 1:x]
6-element Array{Tuple{Int64,Int64},1}:
 (1, 1)
 (2, 1)
 (2, 2)
 (3, 1)
 (3, 2)
 (3, 3)

关于arrays - 在 Julia 中构建遍历多个索引的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64857778/

相关文章:

arrays - 2个哈希数组之间的算术运算

ruby-on-rails - Rails 4 Postgresql 数组数据类型 : updating values

graph - 如何在 LightGraphs (Julia) 中向图形添加自由边?

memory - 在 Julia 中逐行检查内存分配

php - 数组输入值在 mysql 中插入空记录

c - 后缀评估 C

julia - 在 Julia 中修改包中的函数

list - 得到 "Fatal error: Index out of range": show index in list item for swiftui

r - 如何找到满足多个条件的 R 列表的索引