简体   繁体   English

将电影标题和组导演的文本文件一起读入字典

[英]Read text file of movie titles and group directors together into dictionary

I need to create a dictionary with the movie titles and directors below.我需要用下面的电影标题和导演创建一个字典。 It will open a txt file and populate the dictionary with the input below.它将打开一个 txt 文件并使用以下输入填充字典。 The input file of the dictionary is below.字典的输入文件如下。 How could I create this using just d {} for dictionary.我怎么能只使用d {}来创建这个字典。 I have read about split() but am still confused about delimiters.我已经阅读了split()但仍然对分隔符感到困惑。

'Breaker' Morant (1980)/Brown, Bryan (I)/Henderson, Dick (II)/Gray, Patrika/French, Leigh
'Crocodile' Dundee II (1988)/Jbara, Gregory/Holt, Rita/Forbes, Angelyn

It seems that you know there is a movie (key) when there is a ' title ' and the names are delimited by /Last , First/Last , First .当有一个“标题”并且名称由/Last , First/Last , First分隔时,您似乎知道有一部电影(键)。 I also seems as if the end of the movie directors is when a \\n appears.我也好像电影导演的结尾是\\n出现的时候。

The Expected outcome would :预期结果将:

{"'Breaker' Morant (1980)": { 'Brown, Bryan (I)', 'Henderson, Dick (II)', 'Gray, Patrika', 'French, Leigh'}, 
 "'Crocodile' Dundee II (1988)": {'Jbara, Gregory', 'Holt, Rita', 'Forbes, Angelyn'}

.split('delimiter') converts a string into a list with the elements separated by the provided delimiter. .split('delimiter') 将字符串转换为列表,其中元素由提供的分隔符分隔。

For example:例如:

mystr = "oen/two/three"
mylst = mystr.split('/') #the delimiter is '/'

Here, mylst will be: ['one', 'two', 'three']在这里, mylst将是: ['one', 'two', 'three']

So if you want to create a dictionary as per your requirements, you can first create a list and then go for the dictionary by slicing the list.因此,如果您想根据您的要求创建字典,您可以先创建一个列表,然后通过对列表进行切片来查找字典。

movieInfo = "Crocodile Dundee II (1988)/Jbara, Gregory/Holt, Rita/Forbes, Angelyn"
myDict = {}
movieList = movieInfo.split('/')

myDict[movieList[0]] = movieList[1:]

At the end you will get the myDict as {'Crocodile Dundee II (1988)': ['Jbara, Gregory', 'Holt, Rita', 'Forbes, Angelyn']}最后你会得到myDict作为{'Crocodile Dundee II (1988)': ['Jbara, Gregory', 'Holt, Rita', 'Forbes, Angelyn']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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