解析时出乎意料的EOF-如何修复代码?
我已经
desc = ['(4,1);(1,4)', '(2,3);(3,2)', '(4,2);(2,4);(1,3);(3,1)', '(1,2);(2,1);(4,3);(3,4)']
并且希望输出已经
[[(4, 1), (1, 4)], [(2, 3), (3, 2)], [(4, 2), (2, 4), (1, 3), (3, 1)], [(1, 2), (2, 1), (4, 3), (3, 4)]]
尝试过了:
for x in range(len(desc)):
desc[x] = desc[x].split(';')
for y in range(len(desc[x])):
desc[x][y] = eval(desc[x][y])
但是有一个语法错误,说“解析时出乎意料的EOF”。如何修复代码?
在我的代码的最后两行中,我只是试图从包含它们的字符串中提取元素,除了eval()之外,还有其他可以使用的其他东西吗?
I've got
desc = ['(4,1);(1,4)', '(2,3);(3,2)', '(4,2);(2,4);(1,3);(3,1)', '(1,2);(2,1);(4,3);(3,4)']
and I want the output to be
[[(4, 1), (1, 4)], [(2, 3), (3, 2)], [(4, 2), (2, 4), (1, 3), (3, 1)], [(1, 2), (2, 1), (4, 3), (3, 4)]]
So far I've tried:
for x in range(len(desc)):
desc[x] = desc[x].split(';')
for y in range(len(desc[x])):
desc[x][y] = eval(desc[x][y])
but there is a syntax error saying 'unexpected EOF while parsing. How do I fix my code?
For the last two lines of my code I was just trying to extract the tuples from the strings containing them, is there anything else I could use except for eval()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
意外的EOF是由第二个循环的缩进引起的。
这就是应该的样子:
Unexpected EOF is caused by the indentation of the second for loop.
This is how it should look like:
您想用分隔符';'将列表的每个项目拆分。您需要解析列表:
desc 中元素的
,并根据此分隔符将每个元素分开:
temp = element.split(';')
。然后,您可以将列表添加到输出列表[temp [0],temp [1]]
以删除
'''
您必须将项目转换为带有带有的项目内部整数:You want to split each item of your list with the separator ';'. You need to parse your list :
for element in desc
and split each element according to this separator :temp = element.split(';')
. You can then add to your output list the list[temp[0], temp[1]]
To remove the
''
you have to transform your items into actual tuples with the integers inside :