在 Python 中查找上一行的一部分是否等于当前行的一部分
find if part of previous line is equal to part of current line in Python
我对我的代码有疑问。如果我上一行的特定部分等于当前行中的特定部分(在本例中为 Z 部分),我想 return 为真。我的文件如下所示:
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。所以这里是 True。只要上一行中的值不等于当前行中的值,我就想要一个 False。所以第 4 行就是这种情况(328.5575 不等于 268.4040)。这个文件名为 "pointZ.gcode" 并且有很多行。谁能帮我 Python 代码 return 我想要什么?谢谢!
到目前为止我的代码:
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")
这给了我错误:IndexError:列表索引超出范围
我不会post这里的代码,希望你试试。但是会给你提示:
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)
我对我的代码有疑问。如果我上一行的特定部分等于当前行中的特定部分(在本例中为 Z 部分),我想 return 为真。我的文件如下所示:
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。所以这里是 True。只要上一行中的值不等于当前行中的值,我就想要一个 False。所以第 4 行就是这种情况(328.5575 不等于 268.4040)。这个文件名为 "pointZ.gcode" 并且有很多行。谁能帮我 Python 代码 return 我想要什么?谢谢!
到目前为止我的代码:
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")
这给了我错误:IndexError:列表索引超出范围
我不会post这里的代码,希望你试试。但是会给你提示:
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)