Python 将列表更改为int
我完全搞不懂如何将列表更改为intPython 将列表更改为int,python,python-3.x,Python,Python 3.x,我完全搞不懂如何将列表更改为int #Creates a list which is populated by whole names and scores whole_names = list() scores = list() for line in lines: # Makes each word a seperate object objects = line.split(" ") # Joins the firs
#Creates a list which is populated by whole names and scores
whole_names = list()
scores = list()
for line in lines:
# Makes each word a seperate object
objects = line.split(" ")
# Joins the first and last name of every line and makes them
their own seperate objects
whole_names.append(" ".join(objects[0:2]))
# Makes the scores of every line an object
scores.append(objects[2:3])
rect = Rectangle(Point(2, y-50), Point(scores[0],y-25))
rect.setFill("darkgreen")
rect.draw(win)
问题是,点(分数[0],y-25))不会填充,因为分数[0]是一个列表,而不是整数,所以从技术上讲它不能是一个坐标,但该列表中分数[0]的实际值将是一个随机数,我不知道它将是什么数,但实际上它将是一个整数。那么如何将分数[0]转换为随机整数呢?我试过了
分数=整数(分数),但这根本不起作用。假设
分数[0]
类似于['10']
:
Point(int(scores[0][0]), y-25)
然而,这不是正确的解决方案。要使其更好,请更改此行:
scores.append(objects[2:3])
返回一个序列,返回到:
scores.append(objects[2])
返回项目本身。使用此选项,您只需立即将其转换为整数:
Point(int(scores[0]), y-25)
希望这有帮助 假设
得分[0]
类似于['10']
:
Point(int(scores[0][0]), y-25)
然而,这不是正确的解决方案。要使其更好,请更改此行:
scores.append(objects[2:3])
返回一个序列,返回到:
scores.append(objects[2])
返回项目本身。使用此选项,您只需立即将其转换为整数:
Point(int(scores[0]), y-25)
希望这有帮助
这一行提供了一个1元素序列,这可能不是您想要的。索引而不是切片
scores.append(objects[2])
这一行提供了一个1元素序列,这可能不是您想要的。索引而不是切片
scores.append(objects[2])