Julia - Iterate over dictionary key combinations

Is there a great way to iterate over keyboard shortcuts in a dictionary?

my dictionary has meanings such as:

[1] => [1,2], [2,3] => [15], [3] => [6,7,8], [4,9,11] => [3], ... 

what i need to do is get all key combinations with length 1:nwhere nfx 3 can be

So, as in the example above, I would like to iterate over

[[1], [3], [2,3], [[1],[1,2]], [[3],[2,3]], [4,9,11]]

I know that I can just collect the keys, but my dictionary is quite large, and I'm in the middle of redesigning the entire algorithm, because it starts to change impeccably when n > 3, which significantly reduces the efficiency

tl; dr Is there a way to create a combinatorial iterator from a dictionary without collect-in a dictionary?

+1
source share
1

, . , OrderedDict, ( Dicts , , ).

using Iterators
using DataStructures

od = OrderedDict([1] => [1,2], [2,3] => [15], [3] => [6,7,8], [4,9,11] => [3])

sv = map(length,keys(od))        # store length of keys for quicker calculations
maxmaxlen = sum(sv)              # maximum total elements in good key
for maxlen=1:maxmaxlen           # replace maxmaxlen with lower value if too slow
  @show maxlen
  gsets = Vector{Vector{Int}}()  # hold good sets of key _indices_
  for curlen=1:maxlen
    foreach(x->push!(gsets,x),
     (x for x in subsets(collect(1:n),curlen) if sum(sv[x])==maxlen))
  end
  # indmatrix is necessary to run through keys once in next loop
  indmatrix = zeros(Bool,length(od),length(gsets))
  for i=1:length(gsets)              for e in gsets[i]
      indmatrix[e,i] = true
    end
  end
  # gkeys is the vector of vecotrs of keys i.e. what we wanted to calculate
  gkeys = [Vector{Vector{Int}}() for i=1:length(gsets)]
  for (i,k) in enumerate(keys(od))
    for j=1:length(gsets)
      if indmatrix[i,j]
        push!(gkeys[j],k)
      end
    end
  end
  # do something with each set of good keys
  foreach(x->println(x),gkeys)
end

, ? Julia, , .

--- UPDATE ---

fooobar.com/questions/1663384/...

:

function keysubsets(n,d)
  Task() do
    od = OrderedDict(d)
    sv = map(length,keys(od))        # store length of keys for quicker calculations
    maxmaxlen = sum(sv)              # maximum total elements in good key
    for maxlen=1:min(n,maxmaxlen)    # replace maxmaxlen with lower value if too slow
      gsets = Vector{Vector{Int}}()  # hold good sets of key _indices_
      for curlen=1:maxlen
        foreach(x->push!(gsets,x),(x for x in subsets(collect(1:n),curlen) if sum(sv[x])==maxlen))
      end
      # indmatrix is necessary to run through keys once in next loop
      indmatrix = zeros(Bool,length(od),length(gsets))
      for i=1:length(gsets)              for e in gsets[i]
          indmatrix[e,i] = true
        end
      end
      # gkeys is the vector of vecotrs of keys i.e. what we wanted to calculate
      gkeys = [Vector{Vector{Int}}() for i=1:length(gsets)]
      for (i,k) in enumerate(keys(od))
        for j=1:length(gsets)
          if indmatrix[i,j]
            push!(gkeys[j],k)
          end
        end
      end
      # do something with each set of good keys
      foreach(x->produce(x),gkeys)
    end
  end
end

4 ( StackOverflow):

julia> nt2 = NewTask(keysubsets(4,od))
julia> collect(nt2)
10-element Array{Array{Array{Int64,1},1},1}:
 Array{Int64,1}[[1]]          
 Array{Int64,1}[[3]]          
 Array{Int64,1}[[2,3]]        
 Array{Int64,1}[[1],[3]]      
 Array{Int64,1}[[4,9,11]]     
 Array{Int64,1}[[1],[2,3]]    
 Array{Int64,1}[[2,3],[3]]    
 Array{Int64,1}[[1],[4,9,11]] 
 Array{Int64,1}[[3],[4,9,11]] 
 Array{Int64,1}[[1],[2,3],[3]]

( NewTask StackOverflow).

+1

Source: https://habr.com/ru/post/1663385/


All Articles