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?
CodePudding user response:
You are already iterating over dist_list
, so you might as well use enumerate
and use that index instead of manually maintaining count
:
for index, data in enumerate(data_list, 1):
new.loc[base, f"{index}_dist"], new.loc[base, f"{index}_t-"] = data, new.loc[base, "t-"]
CodePudding user response:
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.