简体   繁体   中英

How to write a function that transforms a triple ['a','b','c'] into a single string “a b c .”

I need some help with a homework assignment that I've been given for this coming Monday. I'm a beginner at programming and task is for me to write a function that transforms a string such as ['a','b','c'] into something like "ab c."

I've been trying this for a while, however I can't seem to figure it out. I would really appreciate it if someone could assist me and show me what my mistake is and what needs to be improved!

This is what I have to transform:

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

and this is my code so far:

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

The outcome is supposed to look like this:

    ['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 .']

However these are the results I'm getting:

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

I really hope this is clear enough and in advance thanks a lot for your help!

Answering the question in the title, you can do this:

import ast

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

The problem is that your function will just return the entire graph instead of converting it to a string.

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

Instead, if you always have exactly three elements, you can use a format string:

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

Example:

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

Result:

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

For your "list of strings of lists of lists of string" format: (a) don't use that if you don't absolutely have to, (b) there seem to be some misplaced quotes in there, (c) after fixing those, you still have a list of lists of lists, ie you can not apply ntriple to eval(l) directly, but to each element in that list:

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

Result:

['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 .']

Using a list comprehension and ast.literal_eval

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

Full code:

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

This results in:

[['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 .']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM