Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reduce the number of lines inside the loop

I have this code:

while count < len(dist_list):
    new.loc[base, "1_dist"],  new.loc[base, "1_t-"]= dist_list[0], new.loc[base, "t-"]
    new.loc[base, "2_dist"],  new.loc[base, "2_t-"]= dist_list[1], new.loc[base, "t-"]
    new.loc[base, "3_dist"],  new.loc[base, "3_t-"]= dist_list[2], new.loc[base, "t-"]
    new.loc[base, "4_dist"],  new.loc[base, "4_t-"]= dist_list[3], new.loc[base, "t-"]
    new.loc[base, "5_dist"],  new.loc[base, "5_t-"]= dist_list[4], new.loc[base, "t-"]
    new.loc[base, "6_dist"],  new.loc[base, "6_t-"]= dist_list[5], new.loc[base, "t-"]
    new.loc[base, "7_dist"],  new.loc[base, "7_t-"]= dist_list[6], new.loc[base, "t-"]
    new.loc[base, "8_dist"],  new.loc[base, "8_t-"]= dist_list[7], new.loc[base, "t-"]
    new.loc[base, "9_dist"],  new.loc[base, "9_t-"]= dist_list[8], new.loc[base, "t-"]
    new.loc[base, "10_dist"],  new.loc[base, "10_t-"]= dist_list[9], new.loc[base, "t-"]
    count += 1

I need to do the same code, but with 100 variables (1_dist, 2_dist… 100_dist). Manually, I will have 100 rows inside the loop. But I’m looking for a way to make this process with fewer rows.

Does anyone help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

while count < len(dist_list):
new.loc[base, f"{count + 1}_dist"],  new.loc[base, f"{count + 1}_t-"]= dist_list[count], new.loc[base, "t-"]
count += 1

I don’t quite understand the code but seeing as you wrote those 10 lines it seems you want to go 1 more that count in 1st 2 string and then in 3rd (when giving index to a list maybe) you want it as same as count.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading