Python:从对象列表中操作参数。
Python : Manipulating Arguments from a list of objects.
所以我有以下代码。 merge_files() 函数基本上合并两个文件(字符串)和 returns 一个列表。此列表用于从对象提供参数。
文件 1
['2.02', '-3.88', '15.25']
['4.40', '-2.36', '14.97']
文件2
['P1']
['P2']
问题是当直接给对象赋值时,函数 grade_compass() 工作正常。当对象从解析器中提取相同的值时,它似乎并没有通过这个函数。
完整代码:
from ast import literal_eval
class Compass:
#initialize the attributes
def __init__(self, coordX, coordY, coordZ, Pos):
self.coordx = coordX
self.coordy = coordY
self.coordz = coordZ
self.coord_pos = Pos
def merge_files(infile1, infile2):
with open(infile1) as f1, open(infile2) as f2:
l3 = [] # this is a list for storing all the lines from both files
f1_lists = (literal_eval(line) for line in f1)
f2_lists = (literal_eval(line) for line in f2)
for l1, l2 in zip(f1_lists, f2_lists):
l3.append(l1 + l2)
return l3 # Return all the lines from both text files
def check_step_xrange(coordx):
if coordx > 0.00 and coordx < 7.99:
return "short"
def check_step_zrange(coordz):
if coordz > 4.40 and coordz < 20.00:
return "short"
# Ignore coordy for now
def grade_compass(coordx, coordy, coordz):
if check_step_xrange(coordx) == "short" and check_step_zrange(coordz) == "short":
compass_degree = "compass degree is short"
return compass_degree
def main():
file1 = "coordinates1.txt"
file2 = "coordinates2.txt"
args = merge_files(file1, file2)
# List of instances
compasslist = [Compass(args[i][0], args[i][1], args[i][2], args[i][3]) for i in range(len(args))]
# I just pull the first instance
print "\nThis is the object from the object list"
print compasslist[0].coordx + ' ' + compasslist[0].coordy + ' ' + compasslist[0].coordz + ' ' + compasslist[0].coord_pos
print grade_compass(compasslist[0].coordx, compasslist[0].coordy, compasslist[0].coordz)
print "\nThis is the object manually given"
h = Compass(2.02, -3.88, 15.25, 'P1')
print h.coordx, h.coordy, h.coordz, h.coord_pos
print grade_compass(h.coordx, h.coordy, h.coordz)
if __name__ == '__main__':
main()
第一个输出returnNone。当手动给出时,它有效。
输出:
This is the object from the object list
2.02 -3.88 15.25 P1
None
This is the object manually given
2.02 -3.88 15.25 P1
compass degree is short
期望的输出:
This is the object from the object list
2.02 -3.88 15.25 P1
compass degree is short
This is the object manually given
2.02 -3.88 15.25 P1
compass degree is short
我猜这是 Python 2.x 而不是 Python 3.x。
在 Python2 中,比较运算符总是成功的——并且字符串总是大于整数(或浮点数)。在Python3中,这是一个TypeError
.
>>> 2 > "2"
False
>>> 2 < "2"
True
>>> 2 < "1"
True
>>> 2 < "-500"
所以,这段代码中的问题基本上是您永远不会将从文件中读取的字符串转换为浮点数。
这是两种方法的区别:
Compass(args[i][0], args[i][1], args[i][2], args[i][3])
Compass(2.02, -3.88, 15.25, 'P1')
在前者中,所有参数都是字符串。所以你需要转换它们(或者解析,如果你不能保证输入总是正确的:
Compass(float(args[i][0]), float(args[i][1]), float(args[i][2]), args[i][3])
所以我有以下代码。 merge_files() 函数基本上合并两个文件(字符串)和 returns 一个列表。此列表用于从对象提供参数。
文件 1
['2.02', '-3.88', '15.25']
['4.40', '-2.36', '14.97']
文件2
['P1']
['P2']
问题是当直接给对象赋值时,函数 grade_compass() 工作正常。当对象从解析器中提取相同的值时,它似乎并没有通过这个函数。
完整代码:
from ast import literal_eval
class Compass:
#initialize the attributes
def __init__(self, coordX, coordY, coordZ, Pos):
self.coordx = coordX
self.coordy = coordY
self.coordz = coordZ
self.coord_pos = Pos
def merge_files(infile1, infile2):
with open(infile1) as f1, open(infile2) as f2:
l3 = [] # this is a list for storing all the lines from both files
f1_lists = (literal_eval(line) for line in f1)
f2_lists = (literal_eval(line) for line in f2)
for l1, l2 in zip(f1_lists, f2_lists):
l3.append(l1 + l2)
return l3 # Return all the lines from both text files
def check_step_xrange(coordx):
if coordx > 0.00 and coordx < 7.99:
return "short"
def check_step_zrange(coordz):
if coordz > 4.40 and coordz < 20.00:
return "short"
# Ignore coordy for now
def grade_compass(coordx, coordy, coordz):
if check_step_xrange(coordx) == "short" and check_step_zrange(coordz) == "short":
compass_degree = "compass degree is short"
return compass_degree
def main():
file1 = "coordinates1.txt"
file2 = "coordinates2.txt"
args = merge_files(file1, file2)
# List of instances
compasslist = [Compass(args[i][0], args[i][1], args[i][2], args[i][3]) for i in range(len(args))]
# I just pull the first instance
print "\nThis is the object from the object list"
print compasslist[0].coordx + ' ' + compasslist[0].coordy + ' ' + compasslist[0].coordz + ' ' + compasslist[0].coord_pos
print grade_compass(compasslist[0].coordx, compasslist[0].coordy, compasslist[0].coordz)
print "\nThis is the object manually given"
h = Compass(2.02, -3.88, 15.25, 'P1')
print h.coordx, h.coordy, h.coordz, h.coord_pos
print grade_compass(h.coordx, h.coordy, h.coordz)
if __name__ == '__main__':
main()
第一个输出returnNone。当手动给出时,它有效。
输出:
This is the object from the object list
2.02 -3.88 15.25 P1
None
This is the object manually given
2.02 -3.88 15.25 P1
compass degree is short
期望的输出:
This is the object from the object list
2.02 -3.88 15.25 P1
compass degree is short
This is the object manually given
2.02 -3.88 15.25 P1
compass degree is short
我猜这是 Python 2.x 而不是 Python 3.x。
在 Python2 中,比较运算符总是成功的——并且字符串总是大于整数(或浮点数)。在Python3中,这是一个TypeError
.
>>> 2 > "2"
False
>>> 2 < "2"
True
>>> 2 < "1"
True
>>> 2 < "-500"
所以,这段代码中的问题基本上是您永远不会将从文件中读取的字符串转换为浮点数。
这是两种方法的区别:
Compass(args[i][0], args[i][1], args[i][2], args[i][3])
Compass(2.02, -3.88, 15.25, 'P1')
在前者中,所有参数都是字符串。所以你需要转换它们(或者解析,如果你不能保证输入总是正确的:
Compass(float(args[i][0]), float(args[i][1]), float(args[i][2]), args[i][3])