所以我开始放入一个文件,其中列出了标题,演员,标题,演员等
12 Years a Slave
Topsy Chapman
12 Years a Slave
Devin Maurice Evans
12 Years a Slave
Brad Pitt
12 Years a Slave
Jay Huguley
12 Years a Slave
Devyn A. Tyler
12 Years a Slave
Willo Jean-Baptiste
American Hustle
Christian Bale
American Hustle
Bradley Cooper
American Hustle
Amy Adams
American Hustle
Jeremy Renner
American Hustle
Jennifer Lawrence
我需要做一个字典,看起来像下面的,并列出所有的演员在电影
{'Movie Title': ['All actors'], 'Movie Title': ['All Actors]}
到目前为止我只有这个
d = {}
with open(file), 'r') as f:
for key in f:
d[key.strip()] = next(f).split()
print(d)
使用defaultdict
通常是更好的选择:
from collections import defaultdict
data = defaultdict(list)
with open("filename.txt", 'r') as f:
stripped = map(str.strip, f)
for movie, actor in zip(stripped, stripped):
data[movie].append(actor)
print(data)
所以您需要在读取标题和从输入数据读取演员之间切换。您还需要存储标题,以便您可以在actor行中使用它。
你可以使用标题的设置在阅读标题和阅读演员之间切换。
一些关键检查,你有工作逻辑。
# pretty printer to make the output nice
from pprint import pprint
data = """ 12 Years a Slave
Topsy Chapman
12 Years a Slave
Devin Maurice Evans
12 Years a Slave
Brad Pitt
12 Years a Slave
Jay Huguley
12 Years a Slave
Devyn A. Tyler
12 Years a Slave
Willo Jean-Baptiste
American Hustle
Christian Bale
American Hustle
Bradley Cooper
American Hustle
Amy Adams
American Hustle
Jeremy Renner
American Hustle
Jennifer Lawrence"""
result = {}
title = None
for line in data.splitlines():
# clean here once
line = line.strip()
if not title:
# store the title
title = line
else:
# check if title already exists
if title in result:
# if yes, append actor
result[title].append(line)
else:
# if no, create it with new list for actors
# and of course, add the current line as actor
result[title] = [line]
# reset title to None
title = None
pprint(result)
{'12 Years a Slave': ['Topsy Chapman',
'Devin Maurice Evans',
'Brad Pitt',
'Jay Huguley',
'Devyn A. Tyler',
'Willo Jean-Baptiste'],
'American Hustle': ['Christian Bale',
'Bradley Cooper',
'Amy Adams',
'Jeremy Renner',
'Jennifer Lawrence']}
编辑
从文件中读取时,您需要做的稍微不同。
from pprint import pprint
result = {}
title = None
with open("somefile.txt") as infile:
for line in infile.read().splitlines():
line = line.strip()
if not title:
title = line
else:
if title in result:
result[title].append(line)
else:
result[title] = [line]
title = None
pprint(result)