减少循环内的行数

发布于 01-16 15:19 字数 1069 浏览 2 评论 0原文

我有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你2025-01-23 15:19:15

您已经在迭代 dist_list,因此您不妨使用 枚举并使用该索引,而不是手动维护计数

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-"]

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-"]
故乡的云2025-01-23 15:19:15
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

我不太明白代码,但看到你写的这 10 行,似乎你想要在第 1 个 2 字符串中再添加 1 个计数,然后在第 3 个字符串中(可能为列表提供索引时)你希望它与计数相同。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文