如何编写将三重 ['a'、'b'、'c'] 转换为单个字符串的函数"a b c ."



我需要一些帮助来完成下周一的家庭作业。我是编程的初学者,任务是编写一个函数,将[a","b","c"]等字符串转换为"a b c"之类的字符串。

我已经尝试了一段时间,但是我似乎无法弄清楚。如果有人能帮助我并向我展示我的错误是什么以及需要改进的地方,我将不胜感激!

这是我必须转换的内容:

content = ["[['a','b','c'],['a','b1','c2'],['a2','b2','c']]n",         
'[['Spain','name','"Spain"'],['Spain','capital','Madrid'],
['Madrid','a','Capital']] n', 
'[['Spain','name','"Spain"'], 
['Spain','capital','Madrid'],
['Madrid','a','Capital'],['Capital','a','City'],    
['Spain','neighbours','France'],['Spain','a','Country']] n']

这是我到目前为止的代码:

def makesimple(triple):
## It is a suggestion to first write a function that transforms the triples ...
for i in content:
v = i.split("n")
ii = "t".join(v)
pass
def ntriple(graph):
## ... and then loops through all triples in the graph
for i in range(len(graph)):
return(graph[i:])       
pass
for l in content:
print(ntriple(eval(l.strip())))

结果应该看起来像这样:

['a b c .', 'a b1 c2 .', 'a2 b2 c .']
['Spain name "Spain" .', 'Spain capital Madrid .', 'Madrid a Capital .']
['Spain name "Spain" .', 'Spain capital Madrid .', 'Madrid a Capital .', 'Capital a City .', 'Spain neighbours France .', 'Spain a Country .']

但是,这些是我得到的结果:

[['a', 'b', 'c'], ['a', 'b1', 'c2'], ['a2', 'b2', 'c']]
[['Spain', 'name', '"Spain"'], ['Spain', 'capital', 'Madrid'],     ['Madrid', 'a', 'Capital']]
[['Spain', 'name', '"Spain"'], ['Spain', 'capital', 'Madrid'], ['Madrid', 'a', 'Capital'], ['Capital', 'a', 'City'], ['Spain', 'neighbours', 'France'], ['Spain', 'a', 'Country']]

我真的希望这足够清楚,并提前感谢您的帮助!

回答标题中的问题,您可以这样做:

import ast
s = "['a','b','c']"
l = ast.literal_eval(s)
' '.join(l) + ' .'

问题是你的函数只会返回整个graph,而不是将其转换为字符串。

def ntriple(graph):
for i in range(len(graph)):
return(graph[i:]) # return `graph` from 0 to the end
pass              # this part is never reached, neither is the rest of the loop

相反,如果始终只有三个元素,则可以使用format字符串:

def ntriple(graph):
return "{} {} {} .".format(*graph)

例:

content = [['Spain', 'name', 'Spain'], ['Spain', 'capital', 'Madrid'], ['Madrid', 'a', 'Capital']]
for l in content:
print(ntriple(l))

结果:

Spain name Spain .
Spain capital Madrid .
Madrid a Capital .

对于您的"字符串列表字符串列表"格式:(a(如果您不是绝对必须使用它,请不要使用它,(b(那里似乎有一些放错位置的引号,(c(修复这些后,您仍然有一个列表,即您不能直接将ntriple应用于eval(l), 但对于该列表中的每个元素:

content = ["[['a','b','c'],['a','b1','c2'],['a2','b2','c']]n",         
"[['Spain','name','Spain'],['Spain','capital','Madrid'], ['Madrid','a','Capital']] n", 
"[['Spain','name','Spain'],['Spain','capital','Madrid'],['Madrid','a','Capital'],['Capital','a','City'],    ['Spain','neighbours','France'],['Spain','a','Country']] n"]
for l in content:
lst = eval(l.strip())
print([ntriple(x) for x in lst])

结果:

['a b c .', 'a b1 c2 .', 'a2 b2 c .']
['Spain name Spain .', 'Spain capital Madrid .', 'Madrid a Capital .']
['Spain name Spain .', 'Spain capital Madrid .', 'Madrid a Capital .', 'Capital a City .', 'Spain neighbours France .', 'Spain a Country .']

使用列表推导和ast.literal_eval

[[' '.join(l) + ' .' for l in literal_eval(elem)] for elem in content]

完整代码:

from ast import literal_eval
content = ["[['a','b','c'],['a','b1','c2'],['a2','b2','c']]n",
'[['Spain','name','"Spain"'],['Spain','capital','Madrid'], ['Madrid','a','Capital']] n',
'[['Spain','name','"Spain"'],['Spain','capital','Madrid'],['Madrid','a','Capital'],['Capital','a','City'],['Spain','neighbours','France'],['Spain','a','Country']] n']
print([[' '.join(l) + ' .' for l in literal_eval(elem)] for elem in content])

这导致:

[['a b c .', 'a b1 c2 .', 'a2 b2 c .'], ['Spain name "Spain" .', 'Spain capital Madrid .', 'Madrid a Capital .'], ['Spain name "Spain" .', 'Spain capital Madrid .', 'Madrid a Capital .', 'Capital a City .', 'Spain neighbours France .', 'Spain a Country .']]

相关内容

  • 没有找到相关文章

最新更新