在下周一的作业中,我需要一些帮助。我是编程的初学者,我的任务是编写一个函数,该函数将['a','b','c']之类的字符串转换为类似“ a b c”的内容。
我已经尝试了一段时间,但是似乎无法弄清楚。如果有人可以帮助我并向我展示我的错误是什么以及需要改进的地方,我将不胜感激!
这就是我要转换的:
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']]
但是这些都是我得到的结果:
requestAnimationFrame()
我真的希望这已经足够清楚了,在此先感谢您的帮助!
答案 0 :(得分:1)
回答标题中的问题,您可以这样做:
import ast
s = "['a','b','c']"
l = ast.literal_eval(s)
' '.join(l) + ' .'
答案 1 :(得分:0)
问题在于您的函数只会返回整个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 .']
答案 2 :(得分:0)
使用列表理解和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 .']]