0

我需要一些帮助来完成下周一的家庭作业。我是编程初学者,我的任务是编写一个函数,将 ['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']]

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

4

3 回答 3

1

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

import ast

s = "['a','b','c']"
l = ast.literal_eval(s)
' '.join(l) + ' .'
于 2019-09-05T15:03:17.647 回答
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)修复后那些,您仍然有一个列表列表,即您不能直接应用于ntripleeval(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 .']
于 2019-09-05T15:14:08.717 回答
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 .']]
于 2019-09-05T15:20:14.050 回答