2016-11-12 54 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

沒問題,我最終設法解決了這個問題。 –

相關問題