我需要一些帮助来完成下周一的家庭作业。我是编程初学者,我的任务是编写一个函数,将 ['a','b','c'] 之类的字符串转换为“ab 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']]
我真的希望这足够清楚,并提前非常感谢您的帮助!