O
O
Oleg Pariyev2021-09-04 07:11:16
Python
Oleg Pariyev, 2021-09-04 07:11:16

How to combine 2 loops and will it optimize the program?

There is a piece of code with loops:

inter_mass_n = {item : 0 for i in mass_q for item in range(i[0]-1, i[1])}

for i in mass_q:
    for j in range(i[0]-1, i[1]):
        inter_mass_n[j] += 1


De facto, the program does extra work, so I would like to immediately do +=1 when creating a dictionary. Is it possible to organize this and will such an option work faster?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-09-04
@dmshar

De facto, the program does not know what. for example

inter_mass_n = {item : 0 for i in mass_q for item in range(i[0]-1, i[1])}

creates a dictionary with all null values.
And here it simply increases these zeros by one.
for i in mass_q:
    for j in range(i[0]-1, i[1]):
        inter_mass_n[j] += 1

What would it mean? Why not immediately assign units?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question