2016-11-12 55 views
0

尝试创建一个函数来读取文件并将其添加到一个有组织的字典中,然后在不更改原始字典的情况下返回它。不知道我是否正确使用多个项目和值。功能:读取文件,然后将多个项目添加到字典

返回:

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

示例文件:

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"''' 

代码我到目前为止有:

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

限制:

  1. 符号顺序:按ASCII码 排序,而不是按字母顺序排列。

  2. 没有导入/集合/模块。基本建立在函数,循环和字典方法。

+0

“ASCII整理顺序”和“字母顺序”有什么区别?你的意思是所有大写字母都写在所有小写字母之前吗?或者这涉及到非字母字符?或两者? –

+0

是的@RoryDaulton所有大写字母都出现在所有小写字母之前。不涉及非字母字符 –

+0

我并没有真正明白你想要做什么,但是'db [int(artist)] = ...'应该引发'ValueError',因为艺术家的名字赢了不是一个整数 - 请尝试澄清你想要做的是什么。您是否正在读取csv-File中的数据并将艺术家的所有绘画作为列表中的元素进行关联? – Maurice

回答

1

正如我们在评论中所讨论的那样,您的主要问题是确定将新绘画放置在艺术家绘画列表的哪个位置,基于它的标题。

在我看来,这是某种家庭作业问题,因为在现实世界中没有这些限制的理由。因此,我不会给你完整的解决方案,但指出你在正确的方向(至少我会尝试)。

你的算法应该是这个样子:

  1. 获取与艺术家为重点和值他的画的列表的名称的字典。每幅画由title,year,height,width,mediacountry组成。

  2. 给出了一组新的artisttitleyearheightwidthmediacountry您检索的,艺术家的工作列表。

  3. 现在你的问题是要找出在哪里添加新的绘画(如果它不存在)。

  4. 您循环上述列表中的所有作品。对于每个条目,您都要检查新作品的title是否应该在当前的title之前使用下面的compare_to-函数插入。如果是(-1),则插入它。如果结果是0它已经在列表中并且您返回字典。如果结果是1,则转到列表中的下一个项目。如果没有更多的项目将其追加到最后。

这是compare_to功能:

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 

我不知道你想如何在比较处理数字,随意将它们添加到abc变量。

+0

感谢您的详细解释。我有一个问题,这适用于任何关键的位置,所以我可以决定接受字典和国家(例如)的名称,以便它建立/返回一个新的字典,其中包含国家匹配给定的国家参数的字典中的所有作品? –

+0

不客气。是的,这是可能的,你可以遍历字典的所有键,并用[list comprehension]过滤列表的值(https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions )。然而,这是另一个问题,我可以在评论中向您解释任何事情;-) – Maurice

+0

没问题,我最终设法解决了这个问题。 –

相关问题