解析时出乎意料的EOF-如何修复代码?

发布于 2025-01-31 12:01:56 字数 552 浏览 4 评论 0原文

我已经

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2025-02-07 12:01:56

意外的EOF是由第二个循环的缩进引起的。

for x in range(len(desc)):
    desc[x] = desc[x].split(';')
        for y in range(len(desc[x])): # this has one tab to much 
            desc[x][y] = eval(desc[x][y])

这就是应该的样子:

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])

Unexpected EOF is caused by the indentation of the second for loop.

for x in range(len(desc)):
    desc[x] = desc[x].split(';')
        for y in range(len(desc[x])): # this has one tab to much 
            desc[x][y] = eval(desc[x][y])

This is how it should look like:

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])
一笑百媚生 2025-02-07 12:01:56

您想用分隔符';'将列表的每个项目拆分。您需要解析列表:

desc 中元素的,并根据此分隔符将每个元素分开:
temp = element.split(';')。然后,您可以将列表添加到输出列表[temp [0],temp [1]]

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)']
output = []

for element in desc:
    temps = element.split(";")
    output.append([temps[0], temps[1]])

print(output)
# [['(4,1)', '(1,4)'], ['(2,3)', '(3,2)'], ['(4,2)', '(2,4)'], ['(1,2)', '(2,1)']]

以删除'''您必须将项目转换为带有带有的项目内部整数:

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)']
output = []

for element in desc:
    temps = element.split(";")
    tuples_to_add = []
    for i in temps:
        tuples_to_add.append(tuple([int(i.strip('()')[0]), int(i.strip('()')[-1])]))

    output.append(tuples_to_add)

print(output)
[[(4, 1), (1, 4)], [(2, 3), (3, 2)], [(4, 2), (2, 4), (1, 3), (3, 1)], [(1, 2), (2, 1), (4, 3), (3, 4)]]

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]]

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)']
output = []

for element in desc:
    temps = element.split(";")
    output.append([temps[0], temps[1]])

print(output)
# [['(4,1)', '(1,4)'], ['(2,3)', '(3,2)'], ['(4,2)', '(2,4)'], ['(1,2)', '(2,1)']]

To remove the '' you have to transform your items into actual tuples with the integers inside :

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)']
output = []

for element in desc:
    temps = element.split(";")
    tuples_to_add = []
    for i in temps:
        tuples_to_add.append(tuple([int(i.strip('()')[0]), int(i.strip('()')[-1])]))

    output.append(tuples_to_add)

print(output)
[[(4, 1), (1, 4)], [(2, 3), (3, 2)], [(4, 2), (2, 4), (1, 3), (3, 1)], [(1, 2), (2, 1), (4, 3), (3, 4)]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文