如何在嵌套循环中打印一次迭代结果

huangapple go评论93阅读模式
英文:

How to get one iteration printed in a nested loop

问题

我在Python中有一个嵌套循环:

append_str = "//mycomment "

for item in runfile:   
    for item1 in flat_common_list: 
        item1 = item1[1:-1]  ## 移除双引号,否则匹配不起作用
        if item1 in item:
            pre_comment = append_str + item 
            runfile_final.write(pre_comment)
        else:
            runfile_final.write(item)

我的目标是将所有 "runfile" 中的 "item" 一次性打印出来,但在匹配到 "flat_common_list" 中的 "item1" 行之前,用 //mycomment 前缀。最终将结果写入 runfile_final

根据当前的代码,它在第一次匹配后工作,然后就会中断。如果不添加 break,则在嵌套循环中会重复所有内容。

英文:

I have a nested loop in python:

append_str = "//mycomment "

for item in runfile:   
	for item1 in flat_common_list: 
		item1 = item1[1:-1]  ## remove double quotes otherwise match wont work
		if item1 in item:
			pre_comment = append_str + item 
			runfile_final.write(pre_comment)
		else:
			runfile_final.write(item)
			break

My goal is to print all "items" in "runfile" once but prefix //mycomment to the matching lines with item1 in flat_common_list. Eventually write the results to runfile_final.

With the current code, it's working but only for the first match and then breaks. If I don't add a break, then everything is repeated in nested loop.

答案1

得分: 0

你需要稍微重新组织你的循环。在内部循环结束后,跟踪你想要写入`runfile_final`的值,然后再写入它:

for item in runfile:
modified_item = item
for item1 in flat_common_list:
item1 = item1[1:-1]
if item1 in item:
modified_item = append_str + item
break
runfile_final.write(modified_item)

英文:

You need to organise your loops a bit differently. Keep track of the value you want to write to runfile_final, then write it once you are out of the inner loop:

for item in runfile:
    modified_item = item
    for item1 in flat_common_list:
        item1 = item1[1:-1]
        if item1 in item:
            modified_item = append_str + item
            break
    runfile_final.write(modified_item)

答案2

得分: 0

将您的break移到与项目匹配的情况下,然后取消缩进您的else,以便仅在for不中断时运行。如果有项目匹配,您只写一次,然后中断。如果没有匹配项,else会打印原始项目。我将引号从循环中剥离出来,所以只做一次。

append_str = "//mycomment "

# 从常见列表值中剥离引号以备后续匹配
fixed_common_list = [val[1:-1] for val in flat_common_list]

for item in runfile:
    for item1 in flat_common_list:
        if item1 in item:
            pre_comment = append_str + item
            runfile_final.write(pre_comment)
            break
    else:
        runfile_final.write(item)
英文:

Move your break up to the case where the item matches and then dedent your else so that it only runs if the for doesn't break. If an item matches, you write once then break. If no items match, the else prints the original item. I moved stripping the quotes out of the loop so it is only done once.

append_str = "//mycomment "

# strip quotes from common list values for later match
fixed_common_list = [val[1:-1] for val in flat_common_list]

for item in runfile:   
    for item1 in flat_common_list: 
        if item1 in item:
            pre_comment = append_str + item 
            runfile_final.write(pre_comment)
            break
    else:
        runfile_final.write(item)

huangapple
  • 本文由 发表于 2023年2月16日 08:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定