减少循环内的行数
我有这样的代码:
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
我需要执行相同的代码,但有 100 个变量(1_dist、2_dist...100_dist)。手动地,我将在循环内有 100 行。但我正在寻找一种方法来减少此过程的行数。
有人帮助我吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您已经在迭代
dist_list
,因此您不妨使用枚举
并使用该索引,而不是手动维护计数
:You are already iterating over
dist_list
, so you might as well useenumerate
and use that index instead of manually maintainingcount
: