2

I want to obtain output like

{'episodes': [{'season': 1, 'plays': 0, 'episode': 11}, {'season': 2, 'plays': 0, 'episode': 1}], 'title': 'SHOWNAME1', 'imdb_id': 'tt1855924'} 
{'episodes': [{'season': 4, 'plays': 0, 'episode': 11}, {'season': 5, 'plays': 0, 'episode': 4}], 'title': 'SHOWNAME2', 'imdb_id': 'tt1855923'} 
{'episodes': [{'season': 6, 'plays': 0, 'episode': 11}, {'season': 6, 'plays': 0, 'episode': 12}], 'title': 'SHOWNAME3', 'imdb_id': 'tt1855922'}

but I am stuck on the append line as I need to append to a value inside the dictionary. If title is not in the dictionary it creates the first entry for that title

{'episodes': [{'season': 1, 'plays': 0, 'episode': 12}], 'title': 'Third Reich: The Rise & Fall', 'imdb_id': 'tt1855924'}

Then if the same title appears again I want season, episode and plays to be inserted into the existing line. The script would then do the next show and either create a new entry or append again if there is already en entry for that title.... and so on

if 'title' in show and title in show['title']:
    ep = {'episode': episode, 'season': season}
    ep['plays'] = played
    ?????????????????????.append(ep)
else:
    if imdb_id:
        if imdb_id.startswith('tt'):
            show['imdb_id'] = imdb_id
    if thetvdb != "0":
        show['tvdb_id'] = thetvdb

    if title:
        show['title'] = title
    ep = {'episode': episode, 'season': season}
    ep['plays'] = played
    show['episodes'].append(ep)

Thanks Martijn Pieters, I now have this

    if title not in shows:
        show = shows[title] = {'episodes': []}  # new show dictionary
    else:
        show = shows[title]
    if 'title' in show and title in show['title']:
            ep = {'episode': episode, 'season': season}
            ep['plays'] = played
            show['episodes'].append(ep)
    else:

This give me the output I wanted but just wanted to make sure it looked correct

jhmiller
  • 99
  • 1
  • 8
  • You have a dictionary. Not a list. Only the value is a list. – Martijn Pieters Aug 10 '13 at 10:14
  • What is the name of the outer dictionary? `show`? You are already appending to `show['episodes']` later on in your code. What is exactly the problem here? – Martijn Pieters Aug 10 '13 at 10:16
  • Sorry about that, I am still learning. I append later on if the title is not in the dictionary already. the end output would be 1 line for each title, containing all seasons and episodes. – jhmiller Aug 10 '13 at 10:17
  • 1
    Are you perhaps trying to update a specific episode within the list of episodes? Or are you trying to append a *new* episode? You need to provide us with *input* as well as the expected output. – Martijn Pieters Aug 10 '13 at 10:18
  • What is the code before that code? – lecodesportif Aug 10 '13 at 10:22
  • I have a text file, each line has info about a Tv show (Title, episode, season, played, imdb, tvdb) plus other stuff. The file is scanned line by line and passed to the above code, so title is title returned from the file. – jhmiller Aug 10 '13 at 10:24
  • Where do you get the variables `episode`, `season` and `played` from? – lecodesportif Aug 10 '13 at 10:27
  • from the file, 1 line at a time. with code like if "_T\t" in list: title = re.search("\t_T\t(.*?)\t", list).group(1) This is done for each of season, episode etc the the above kicks in. For the first show the else is ran but if the second title is the same I need to add its season, episode and playcount to the dictionary created. – jhmiller Aug 10 '13 at 10:30

1 Answers1

1

You need to store your matches in a dictionary instead, keyed by title. You can then find the same show again if you encounter it in your file more than once:

shows = {}

# some loop producing entries
    if title not in shows:
        show = shows[title] = {'episodes': []}  # new show dictionary
    else:
        show = shows[title]

    # now you have `show` dictionary to work with
    # add episodes directly to `show['episodes']`

After collecting all your shows, use shows.values() to extract all show dictionaries as a list.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343