Python将列表上的二维字符串转换为浮点数
我有一个学校项目,我需要制作一个二维列表,并计算该列表上的平均分。出于某种原因,我无法将列表上的值更改为浮点数,即使它使用print(pointslist[0][1])将它们打印为单个值Python将列表上的二维字符串转换为浮点数,python,Python,我有一个学校项目,我需要制作一个二维列表,并计算该列表上的平均分。出于某种原因,我无法将列表上的值更改为浮点数,即使它使用print(pointslist[0][1])将它们打印为单个值 def read_points(): print("Input the points, one per line as x,y.") print("Stop by entering an empty line.") arvo = 0 pointslist = [] while arvo != "":
def read_points():
print("Input the points, one per line as x,y.")
print("Stop by entering an empty line.")
arvo = 0
pointslist = []
while arvo != "":
arvo = input("")
kordinaatti = arvo.split(",")
pointslist.append(kordinaatti)
return pointslist
def calculate_midpoint(pointslist):
h = len(pointslist)
j = int(0)
summax = 0
summay = 0
while j <= h:
arvox = pointslist[j][0]
arvoy = pointslist[j][1]
summax += float(arvox)
summay += float(arvoy)
summax = float(summax / h)
summay = float(summay / h)
j += 1
return summax, summay
格式有点不正确,但在代码中是正确的
谢谢:)现在我看到了问题,但这部分代码仍然存在问题:
def calculate_midpoint(pointslist):
h = len(pointslist)
j = 0
summax = 0
summay = 0
while j <= h:
arvox = float(pointslist[j][0])
arvoy = float(pointslist[j][1])
summax += float(arvox)
summay += float(arvoy)
summax = float(summax / h)
summay = float(summay / h)
j += 1
return summax, summay
def计算_中点(点列表):
h=len(点列表)
j=0
summax=0
summay=0
而j您的read_points()
返回的值是一个空字符串,float
无法处理该值
如果我读取点()
并输入'5'、'4'、'3'
,则返回['5']、['4']、['3']、['']
当尝试浮点(''')
时,该列表中的最后一项将抛出错误。因此,您需要在read_points()
中修复它,以便只返回输入的而不是空行,或者在第二个函数中处理非整数
因此,代码的另一种替代方法可能是:
def read_points():
print("Input the points, one per line as x,y.")
print("Stop by entering an empty line.")
arvo = 0
pointslist = []
while arvo != "":
arvo = input("")
kordinaatti = arvo.split(",")
pointslist.append(kordinaatti)
return pointslist[:-1]
def calculate_midpoint(pointslist):
h = len(pointslist)-1
j = int(0)
summax = 0
summay = 0
while j <= h:
arvox = pointslist[j][0]
arvoy = pointslist[j][0]
summax += float(arvox)
summay += float(arvoy)
summax = float(summax / h)
summay = float(summay / h)
j += 1
return summax, summay
def read_points():
打印(“输入点,每行一个作为x,y.”)
打印(“通过输入空行停止”)
arvo=0
pointslist=[]
而阿尔沃!="":
arvo=输入(“”)
Kordinatti=arvo.split(“,”)
pointslist.append(kordinaatti)
返回点列表[:-1]
def计算_中点(点列表):
h=len(点列表)-1
j=int(0)
summax=0
summay=0
尽管j忽略了格式问题,但底层代码中有两个bug:
阅读点中的篱笆柱问题
read_points
在读取空行后终止,但它还会将该空行附加到pointslist
,这意味着pointslist
中的最后一个条目无效。有很多方法可以解决这个问题:一个简单的方法是在每次迭代结束时而不是开始时阅读:
def read_points():
print("Input the points, one per line as x,y.")
print("Stop by entering an empty line.")
arvo = 0
pointslist = []
arvo = input("")
while arvo != "":
kordinaatti = arvo.split(",")
print(kordinaatti)
pointslist.append(kordinaatti)
arvo = input("")
return pointslist
这是导致“无法将字符串转换为浮点”问题的原因,因为最后一个点不是有效点
在计算中点时按一个问题关闭
您的代码从j=0
迭代到j=len(pointslist)
,并在每次迭代时尝试访问pointslist[j]
。这试图读取len(pointslist)+1
项,这是不正确的;您最多只能读取j=len(pointslist)-1
。这就是你在评论中提到的索引错误的原因
固定版本:
def calculate_midpoint(pointslist):
print(pointslist)
h = len(pointslist)
j = int(0)
summax = 0
summay = 0
while j < h:
arvox = pointslist[j][0]
arvoy = pointslist[j][0]
summax += float(arvox)
summay += float(arvoy)
summax = float(summax / h)
summay = float(summay / h)
j += 1
return summax, summay
def计算_中点(点列表):
打印(点列表)
h=len(点列表)
j=int(0)
summax=0
summay=0
而j
这是一个良好的开端,但代码中有两个错误
第一个错误是读取输入的代码试图将空行视为浮点
第二个错误是,计算中点的代码试图通过在比较中使用小于
或等于
来处理列表末尾以外的点
我已经修改了你的代码,使其在下面正常工作
def read_2d_points():
'''
Read 2D points from the command line.
'''
print("Input the points, one per line as x,y.")
print("Stop by entering an empty line.")
arvo = 0
pointslist = []
while arvo != "":
arvo = input("? ")
if ',' in arvo:
# Ignore the case where the line is empty
# to avoid a float conversion error.
kordinaatti = [float(x.strip()) for x in arvo.split(",")]
assert len(kordinaatti) == 2 # assume 2D
pointslist.append(kordinaatti)
assert len(pointslist) > 1
return pointslist
def calculate_midpoint(pointslist):
'''
Calculate the mid point.
'''
h = len(pointslist)
j = int(0)
summax = 0
summay = 0
while j < h:
arvox = pointslist[j][0]
arvoy = pointslist[j][1]
summax += float(arvox)
summay += float(arvoy)
summax = float(summax / h)
summay = float(summay / h)
j += 1
return summax, summay
pointsList = read_2d_points()
print('points: {} {}'.format(len(pointsList), pointsList))
print('midpoint: {}'.format(calculate_midpoint(pointsList)))
def read_2d_points():
'''
从命令行读取二维点。
'''
打印(“输入点,每行一个作为x,y.”)
打印(“通过输入空行停止”)
arvo=0
pointslist=[]
而阿尔沃!="":
arvo=输入(“?”)
如果arvo中有“,”,则:
#忽略行为空的情况
#以避免浮点转换错误。
Kordinatti=[arvo.split(“,”)中x的float(x.strip())]
断言len(kordinaatti)==2#假设2D
pointslist.append(kordinaatti)
断言len(pointslist)>1
返回点列表
def计算_中点(点列表):
'''
计算中点。
'''
h=len(点列表)
j=int(0)
summax=0
summay=0
而j
如果您对扩展python知识感兴趣,我建议您考虑添加错误处理(而不是断言),考虑使用classes
或namedtuples
,并可能考虑使用列表理解
祝您好运。上述代码中的–符号在做什么?看起来它现在就在函数定义之后,这在语法上没有意义。arvoy=pointslist[j][0]应该是arvoy=pointslist[j][1]。这会导致另一个错误,即声明其超出索引。谢谢,我使用了if而不是返回值,但现在它在func calculate_midpoint中声明J超出了索引。您是否看到编辑以计算_midpoint?由于索引从0开始,pointslist的长度太长。因此,当j
def read_2d_points():
'''
Read 2D points from the command line.
'''
print("Input the points, one per line as x,y.")
print("Stop by entering an empty line.")
arvo = 0
pointslist = []
while arvo != "":
arvo = input("? ")
if ',' in arvo:
# Ignore the case where the line is empty
# to avoid a float conversion error.
kordinaatti = [float(x.strip()) for x in arvo.split(",")]
assert len(kordinaatti) == 2 # assume 2D
pointslist.append(kordinaatti)
assert len(pointslist) > 1
return pointslist
def calculate_midpoint(pointslist):
'''
Calculate the mid point.
'''
h = len(pointslist)
j = int(0)
summax = 0
summay = 0
while j < h:
arvox = pointslist[j][0]
arvoy = pointslist[j][1]
summax += float(arvox)
summay += float(arvoy)
summax = float(summax / h)
summay = float(summay / h)
j += 1
return summax, summay
pointsList = read_2d_points()
print('points: {} {}'.format(len(pointsList), pointsList))
print('midpoint: {}'.format(calculate_midpoint(pointsList)))