好的,我有一个名为“relationships.txt”的文本文件,其中包含以下内容:
Elizabeth: Peter, Angela, Thomas
Mary: Tom
Angela: Fred, Alison
Alison: Beatrice, Dick, Harry
mother Elizabeth
mother Tom
mother Angela
mother Gurgle
前 4 行设置为 Mother:Child、Child、Child 等。最后 4 行是应该返回结果的语句。例如:
mother Elizabeth
应该返回:Mother not known
尽管
mother Tom
应该返回:Mary
我打算创建一个字典来让它工作,但我不知道该怎么做。帮助表示赞赏。
到目前为止,我有以下内容:
test_file = open('relationships.txt', 'w')
test_file.write('''Elizabeth: Peter, Angela, Thomas
Mary: Tom
Angela: Fred, Alison
Alison: Beatrice, Dick, Harry
mother Elizabeth
mother Tom
mother Angela
mother Gurgle
''')
test_file.close()
def create_list():
open_file = open('relationships.txt', 'r')
lines = open_file.readlines()
return(lines)