-4
the_answer = [
{'Q14r01': ['Q14r01c01', 'Q14r01c02', 'Q14r01c03']},
{'Q14r02': ['Q14r02c01', 'Q14r02c02', 'Q14r02c03']},
{'Q14r03': ['Q14r03c01', 'Q14r03c02', 'Q14r03c03']},
{'Q14r04': ['Q14r04c01', 'Q14r04c02', 'Q14r04c03']},
{'Q14r05': ['Q14r05c01', 'Q14r05c02', 'Q14r05c03']},
{'Q14r06': ['Q14r06c01', 'Q14r06c02', 'Q14r06c03']},
{'Q14r07': ['Q14r07c01', 'Q14r07c02', 'Q14r07c03']},
{'Q14r08': ['Q14r08c01', 'Q14r08c02', 'Q14r08c03']},
{'Q14r09': ['Q14r09c01', 'Q14r09c02', 'Q14r09c03']},
{'Q14r10': ['Q14r10c01', 'Q14r10c02', 'Q14r10c03']},
{'Q14r11': ['Q14r11c01', 'Q14r11c02', 'Q14r11c03']},
{'Q14r12': ['Q14r12c01', 'Q14r12c02', 'Q14r12c03']},
{'Q14r13': ['Q14r13c01', 'Q14r13c02', 'Q14r13c03']},
{'Q14r14': ['Q14r14c01', 'Q14r14c02', 'Q14r14c03']},
{'Q14r15': ['Q14r15c01', 'Q14r15c02', 'Q14r15c03']},
{'Q14r16': ['Q14r16c01', 'Q14r16c02', 'Q14r16c03']},
{'Q14r17': ['Q14r17c01', 'Q14r17c02', 'Q14r17c03']},
{'Q14r18': ['Q14r18c01', 'Q14r18c02', 'Q14r18c03']},
{'Q14r19': ['Q14r19c01', 'Q14r19c02', 'Q14r19c03']},
{'Q14r20': ['Q14r20c01', 'Q14r20c02', 'Q14r20c03']},
{'Q14r21': ['Q14r21c01', 'Q14r21c02', 'Q14r21c03']},
{'Q14r22': ['Q14r22c01', 'Q14r22c02', 'Q14r22c03']},
{'Q14r23': ['Q14r23c01', 'Q14r23c02', 'Q14r23c03']},
{'Q14r24': ['Q14r24c01', 'Q14r24c02', 'Q14r24c03']},
{'Q14r25': ['Q14r25c01', 'Q14r25c02', 'Q14r25c03']},
{'Q14r26': ['Q14r26c01', 'Q14r26c02', 'Q14r26c03']},
{'Q14r27': ['Q14r27c01', 'Q14r27c02', 'Q14r27c03']}]

This is what i need to get. Can someone please help me? Keep it simple. I'm new to this world of Python so...

Q14r01c0 = "Q14r01c0"
l = ['Q14r01c0'+str(e) for e in range(1,4)]
dictcomp = {('Q14r0'+str(a) for a in range(1,28)) : l}
print(dictcomp)

This is what I`ve tried... I have no idea if this is going to help you or not.

And this is what i have so far: {<generator object <genexpr> at 0x0000000003F81750>: ['Q14r01c01', 'Q14r01c02', 'Q14r01c03']}

Fahim Ahmed
  • 141
  • 11
  • 6
    If you're new to python, why don't you try nested for loops instead of jumping directly to list comprehensions and dict comprehensions? "Keep it simple" ;) – Mike Scotty May 21 '19 at 08:41
  • [This question](https://stackoverflow.com/questions/339007/nicest-way-to-pad-zeroes-to-a-string) might be of some use to you. – meowgoesthedog May 21 '19 at 08:43
  • i need to use dict comprehensions... :( that's why i'm asking for help – Robert Ionescu May 21 '19 at 08:51
  • 1
    @RobertIonescu Notice, that if you use dict comprehension you won't achieve a list of dictionaries. The output will be a dictionary of dictionaries. – Relandom May 21 '19 at 08:55

3 Answers3

1

Actually, you are really close. But you are wrongly using a dictionary of comprehension.

dict_variable = {key:value for (key,value) in iterable}

so instead of

dictcomp = {('Q14r0'+str(a) for a in range(1,28)) : l}

You should use:

dictcomp = {'Q14r0'+str(a): l for a in range(1,28)}

Also, notice that order in a dictionary is random (for python < 3.7) so printing dictionary might have different output than you wrote above.

EDIT: I didn't notice output should be a list of dictionaries (thanks meowgoesthedog).

In that case, you should change the dictionary of comprehension to list of comprehension:

dictcomp = [{'Q14r0'+str(a): l} for a in range(1,28)]
Relandom
  • 1,029
  • 2
  • 9
  • 16
  • 3
    OP's desired output is a *list* of dictionaries each with a single key-value pair, so ordering isn't a necessary consideration here. – meowgoesthedog May 21 '19 at 08:48
0
[{f'Q14r{r:02}': [f'Q14r{r:02}c{c:02}' for c in range(1, 4)]} for r in range(1, 28)]
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
0

try this:

innitial_value = "Q14r"
result_length = 27
list_length = 3
result = [{innitial_value + str(i) : [innitial_value + str(i) +"c"+str(j) for j in range(1,list_length+1)]} for i in range(1,result_length+1)]

the values for innitial_value , result_length and list_length can be changed to get desired results

Faizan Naseer
  • 589
  • 3
  • 12