Home > Enterprise >  How do I add lines to a key and different lines as values?
How do I add lines to a key and different lines as values?

Time:12-01

So I start put with a file that lists title, actor, title, actor, etc.

    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

I need to make a dictionary that looks like what's below and lists all actors in the movie

    {'Movie Title': ['All actors'], 'Movie Title': ['All Actors]}

So far I only have this

d = {}

with open(file), 'r') as f:
    for key in f:
        d[key.strip()] = next(f).split()

print(d)

CodePudding user response:

Using a defaultdict is usually a better choice:

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)

CodePudding user response:

So you need to switch between reading the title and reading the actor from the input data. You also need to store the title, so you can use it in the actor line.

You can use the setting of the title for switching between reading the title and reading the actor.

Some key checking and you have working logic.

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

output

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

EDIT

when reading from a file, you need to do it slightly different.

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)
  • Related