Function: Read a file then add multiple items to dictionary

匿名 (未验证) 提交于 2019-12-03 02:42:02

问题:

Trying create a function to read a file and add additional multiple items to it into an organized dictionary then return it without changing the original dictionary. Not sure if I'm doing it correctly with the multiple items and values.

Returns:

{'Leonardo da Vinci': [("Portrait of Isabella d'Este", 1499, 63.0, 46.0, 'chalk', 'France'), ('The Last Supper', 1495, 460.0, 880.0, 'tempera', 'Italy')], 'Pablo Picasso': [('Guernica', 1937, 349.0, 776.0, 'oil paint', 'Spain')]}

Example file:

file1='''"Artist","Title","Year","Total Height","Total Width","Media","Country" "Pablo Picasso","Guernica","1937","349.0","776.0","oil paint","Spain" "Leonardo da Vinci","The Last Supper","1495","460.0","880.0","tempera","Italy"'''

Code I have so far:

def add_work (db,artist,title,year,height,width,media,country): db = {}     with open(filename) as f:     for line in f:         (title, year, height, width, media, country) = line.split()         db[int(artist)] = (title, year, height, width, media, country)         for i in d.keys():             if i == artist #If artist in dictionary, then add it to item.                 db[i].extend             elif i == title #If it has the same title as in the database, its a duplicate so return none.                 return None add_work(d1,"Leonardo   da  Vinci","Portrait of Isabella d'Este", 1499, 63.0,46.0, "chalk", "France")

Restrictions:

  1. Asciibetical order: Is sorted in ASCII collated order rather than alphabetical order.

  2. No imports/collections/modules. Just basic built in functions, loops, and dict methods.

回答1:

As we discussed in the comments, your main problem is figuring out where to place the new painting in the list of an artist's paintings based on it's title.

It appears to me, that this is some kind of a homework question, since there is no reason for these constraints in a real world setting. Because of that I'm not going to give you the full solution, but point you in the right direction (at least I'll try to).

Your algorithm should look something like this:

  1. Get a dictionary with the name of the artist as the key and a list of his paintings as values. Each painting consists of title, year, height, width, media and country.

  2. Given a new set of artist, title, year, height, width, media and country you retrieve the list of that artists work.

  3. Now your problem is to find out where to add the new painting (if it doesn't already exist).

  4. You loop through all paintings in the aforementioned list. For each entry you check if the title of the new work should be inserted before the current title using the compare_to-function below. If yes (-1) you insert it. If the result is 0 it is already in the list and you return the dictionary. If the result is 1 you move on to the next item of the list. If there are no more items you append it to the end.

This is the compare_to function:

def compare_to(string_1, string_2):     """     This functions returns -1 if string_1 should be inserted before string_2,     0 if the strings are the same (in which case it doesn't matter - or this     shouldn't happen) and 1 if string_1 is supposed to be inserted after     string_2.     """     abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"     if string_1 == string_2:         return 0      for i in range(min(len(string_1), len(string_2))):         if abc.index(string_1[i]) < abc.index(string_2[i]):             return -1      # The strings are not the same, the shorter one should come first     if len(string_2) > len(string_1):         return -1      return 1

I don't know how you'd like to handle numbers in the comparison, feel free to add them to the abc variable.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!