在Python中查找前一行的部分是否等于当前行的部分
我有一个关于我的代码的问题。如果前一行的特定部分等于当前行中的特定部分(在本例中为Z部分),我希望返回True。我的文件如下所示:在Python中查找前一行的部分是否等于当前行的部分,python,line,equals,Python,Line,Equals,我有一个关于我的代码的问题。如果前一行的特定部分等于当前行中的特定部分(在本例中为Z部分),我希望返回True。我的文件如下所示: G17 G3 X387.9385 Y200.0000 Z268.4040 R187.9385 G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385 G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385 G17 G3 X200.0000 Y353.2089 Z328.557
G17 G3 X387.9385 Y200.0000 Z268.4040 R187.9385
G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385
G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385
G17 G3 X200.0000 Y353.2089 Z328.5575 R153.2089
所以在这种情况下,如果第2行中“Z”(268.4040)后面的值等于第1行中的那部分,我想要一个True。所以这在这里是真实的。只要前一行中的值与当前行中的值不相等,我就想要一个False。第4行就是这样(328.5575不等于268.4040)。该文件名为“pointZ.gcode”,有许多行。有人能帮我用Python代码返回我想要的东西吗?谢谢
到目前为止,我的代码是:
q = open("pointZ.gcode", "r")
searchlines = q.readlines()
file = ""
for i, line in enumerate(searchlines):
if "Z" in line:
zp0 = map(str, searchlines[i+0].split()[4:5])
zp1 = map(str, searchlines[i+1].split()[4:5])
if zp0 == zp1:
print("T")
else:
print("F")
这给了我一个错误:indexer错误:列表索引超出范围我不会在这里发布代码,希望您尝试一下。但我会给你一些提示:
1)Read the line from the file.
2) Split it on basis of " ".
3) The 4th element of the list is what you want.
4) Now check it with previous string. You will need to maintain a variable where you will have to store the previous string. Initially it can be null.
5) If it matches, print True else print False
我不会在这里发布代码,希望你试试。但我会给你一些提示:
1)Read the line from the file.
2) Split it on basis of " ".
3) The 4th element of the list is what you want.
4) Now check it with previous string. You will need to maintain a variable where you will have to store the previous string. Initially it can be null.
5) If it matches, print True else print False
一种可能的解决方案是,不从文件中读取数据,而是显示一种将结果累加到列表中的基本算法,列表是长度
N-1
,其中N
是行数
lines=['G17 G3 X387.9385 Y200.0000 Z268.4040 R187.9385',
'G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385',
'G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385',
'G17 G3 X200.0000 Y353.2089 Z328.5575 R153.2089']
def lines_equal(curr_line, prev_line, compare_char):
curr_line_parts = curr_line.split(' ')
prev_line_parts = prev_line.split(' ')
for item in zip(curr_line_parts, prev_line_parts):
if item[0].startswith(compare_char):
return item[0] == item[1]
results = []
prev_line = lines[0]
for line in lines[1:]:
results.append(lines_equal(line, prev_line, 'Z'))
prev_line = line
print(results)
一种可能的解决方案是,不从文件中读取数据,而是显示一种将结果累加到列表中的基本算法,列表是长度
N-1
,其中N
是行数
lines=['G17 G3 X387.9385 Y200.0000 Z268.4040 R187.9385',
'G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385',
'G17 G3 X200.0000 Y387.9385 Z268.4040 R187.9385',
'G17 G3 X200.0000 Y353.2089 Z328.5575 R153.2089']
def lines_equal(curr_line, prev_line, compare_char):
curr_line_parts = curr_line.split(' ')
prev_line_parts = prev_line.split(' ')
for item in zip(curr_line_parts, prev_line_parts):
if item[0].startswith(compare_char):
return item[0] == item[1]
results = []
prev_line = lines[0]
for line in lines[1:]:
results.append(lines_equal(line, prev_line, 'Z'))
prev_line = line
print(results)
您现有的代码在哪里?它崩溃了,因为您正在迭代所有行并试图访问下一行(使用代码
searchlines[i+1]
),但最后一行没有下一行。修改代码,从第二行开始迭代,或从第二行到最后一行停止。现有代码在哪里?它崩溃了,因为您正在迭代所有行并试图访问下一行(使用代码搜索行[i+1]
),但最后一行没有下一行。修改代码以在第2行开始迭代,或在第2行到最后一行停止。