-4

How shall I achieve the required output through dictionary comprehensions?

{'R': {0: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   3: {2: 0, 3: 0, 4: 0, 5: 0, 1: 0}},
 'L': {0: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   3: {2: 0, 3: 0, 4: 0, 5: 0, 1: 0}},
 'B': {0: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
   3: {2: 0, 3: 0, 4: 0, 5: 0, 1: 0}}}

I got it through the code below:

d_clas = {'B':{} , 'C':{}, 'D':{}}
l_uniq = [array([1, 2, 3, 4, 5], dtype=int64),
      array([1, 2, 3, 4, 5], dtype=int64),
      array([1, 2, 3, 4, 5], dtype=int64),
      array([2, 3, 4, 5, 1], dtype=int64)]

for i in d_clas:
    c_clas = {}
    for j in range(len(l_uniq)-1):
        c_clas[j] = {}
        for k in l_uniq[j]:
        c_clas[j][k] = 0
    d_clas[i] = c_clas
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Please go through the intro [tour](https://stackoverflow.com/tour), and review [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to understand the community expectations and to help you improve your current and future questions, which can help you get better answers. Please review the [Stack Overflow question checklist](https://meta.stackoverflow.com/q/260648/15497888). – Henry Ecker May 01 '21 at 18:51

1 Answers1

1

Start slow work your way up. I like to start with the inner most item and work outwards.

You started with:

d_clas = {'B':{} , 'C':{}, 'D':{}}
for i in d_clas:
    c_clas = {}
    for j in range(len(l_uniq)-1):
        c_clas[j] = {}
        for k in l_uniq[j]:
            c_clas[j][k] = 0
    d_clas[i] = c_clas

First do the inner most structure:

d_clas = {'B':{} , 'C':{}, 'D':{}}
for i in d_clas:
    c_clas = {}
    for j in range(len(l_uniq)-1):
        c_clas[j] = {k: 0 for k in l_uniq[j]}
    d_clas[i] = c_clas

Then the next one:

d_clas = {'B':{} , 'C':{}, 'D':{}}
for i in d_clas:
    d_clas[i] = {
        j: {k: 0 for k in l_uniq[j]} 
        for j in range(len(l_uniq) - 1)
    }

Finally the last structure:

d_clas = {
    i: {
        j: {k: 0 for k in l_uniq[j]} 
        for j in range(len(l_uniq) - 1)
    }
    for i in ('B', 'C', 'D')
}
flakes
  • 21,558
  • 8
  • 41
  • 88
  • @kalaivananramalingam consider upvoting or marking correct if it addresses your issue! :) – flakes May 01 '21 at 18:59
  • I need 15 reputations it seems. I couldn't up vote. I marked it as correct. – kalaivanan ramalingam May 01 '21 at 19:34
  • Nice, but `('B', 'C', 'D')` needs a closing parenthesis. I realize this borders on trivial, but my personal taste would be to use `for i in 'BCD'` - just as readable and more concise (so long as it's always going to be a single character for the key). – Jamie Deith May 01 '21 at 20:18
  • @JamieDeith Thanks for the catch on the brace. Although strings can be used as an iterable, this pattern breaks down soon as a key needs multiple characters. From a personal preference standpoint, an explicit list is more code, but is also more readable. – flakes May 01 '21 at 20:25