python - 在比较列表时,您可以跳过在上一次迭代中完成的索引吗?
问题描述
我正在尝试比较两个几乎相同的文件,只有一个缺少条目,括号内的内容可能不同。
我正在尝试遍历与较大列表中的每个条目相比的所有较小列表,直到匹配为止,如果匹配则将该行复制到一个新文件中,如果没有则在新文件中写入一个空行然而。问题是在每次迭代中它从头开始我希望它跳过它已经从上一次迭代中检查过的行。有没有办法做到这一点,或者最好从文件中删除行?
#opening of files
fh = open('sampleparsefile.txt', "r")
fh2 = open('sampleparsefile2.txt', "r")
f = open('compared', 'w')
#fhw = open('sampleparsefile.txt', "w")
# Make each line a list split at open '('
line1Split = [line1.split('(')[0] for line1 in fh.readlines()]
line2Split = [line2.split('(')[0] for line2 in fh2.readlines()]
print(line1Split)
print(line2Split)
# iterate through line2Split
# for item2 in line2Split:
for item2 in line2Split:
#splitSingleLine = [aVariable.split('(')[0]]
# Set a variable to false
founditem = False
# iterate through each entry in line1Split comparing
# to item in line2Split
for item1 in line1Split:
# While match is not found
# boolean variable set to false
while (founditem == False):
# if they match write the match to a file and break
# change boolean variable to true if matched
if item1 == item2:
founditem = True
# if there is a newline character strip it
# if there isn't continue on
if '\n' in str(item2):
x = item2.split('\n')[0]
f.write(str(x))
break
else:
f.write(item2)
# WAS HERE but thinking deleting lines from original file would not be good idea
#fhw = open('sampleparsefile.txt', "w")
#for aLines in fhw.readlines:
# if item1 != item2:
# line1Split.pop[0]
break
# also will want to remove found entry from line2split
# that way following passes wont search same first lines
# will also need to re-attach what was in parenthesis
# before writing to file
# if match is not found write a new line to file and break
else:
f.write('\n')
break
fh.close()
fh2.close()
f.close()
#Sampleparsefile.txt sample ignore all #
# Data1
# Data2
# Data3(a)
# Data4(ab)
# Data5(ABC)
# Data6
# Data7
# Data8(a)
# Data9(a)
#Sampleparsefile.txt sample
# Data1
# Data3(a)
# Data5(ABC)
# Data6
# Data8(A)
# Data9(a)
#Desired Result - for now just whats before brackets
# Data1
#
# Data3(ABC)
#
# Data5(A)
# Data6
#
# Data8(a)
# Data9(a)
#Actual Result
# Data1
#
# Data3
#
#
#
# Data5
#
#
#
#
# Data6
#
#
#
#
#
#
# Data8
#
#
#
#
#
#
#
# Data9
解决方案
不要使用第二个 for 循环,而是使用基于递增变量的 while 循环。例如:
i = 0
for item2 in list2:
founditem = False
while i < len(list1):
# your code that goes inside the second for loop
i += 1
希望这对您有所帮助。
推荐阅读
- azure-devops - Azure DevOps 管道工件显示此错误:“没有可用于 PlatformV2 构建的版本...”
- android - 如何显示动态 ExpandableViews
- dynamics-crm - D365 Dynamics Customer Engagement / CRM V9 - 在修改者字段中填充适当的值
- angular - 条件请求作为序列中的第一个请求
- sql - 参数化过程 不同场景
- excel - 如何将公式(文本)的结果作为另一个更大公式的一部分?
- php - 如何从 WordPress 网站中的 php 重新启动数据库
- php - 表单提交到数据库后,如何使用 SMS API 发送消息?
- spring - 由于缺少 ServletWebServerFactory bean,Spring Boot jar 无法启动 Web 服务器
- pandas - pandas:删除重复行,同时保留虚拟变量值