最直接的做法是反转字典。actor2movies = {}for movie, actors in movie2actors.items(): for actor in actors: if actor not in actor2movies: actor2movies[actor] = [] actor2movies[actor].append(movie)(如果改为初始化actor2movies为collections.defaultdict(list),则可以省略该if语句。)然后写出结果字典,一次一个键及其值。with open("foo.txt", "w") as f: for actor, movies in actor2movies.items(): print("{},{}".format(actor, ",".join(movies))