ファイルを読み込んで複数の項目を追加し、整理した辞書に追加して元の辞書を変更せずに戻す機能を作成しようとしています。複数の項目と値で正しく実行しているかどうかは不明です。機能:ファイルを読み込んで複数の項目を辞書に追加する
戻り値:
{'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")
制限:
衛生学的順序:アルファベット順ではなくASCII 照合順序でソートされます。
インポート/コレクション/モジュールがありません。関数、ループ、およびdictメソッドを組み込んだ基本的なもの。
「ASCII照合順」と「アルファベット順」の違いは何ですか?大文字のすべてがすべて小文字の前に来ることを意味しますか?それとも、アルファベット以外の文字に関連していますか?または両方? –
はい@RoryDaultすべての大文字はすべて小文字の前に来ます。アルファベット以外の文字には関係しません –
私はあなたがやろうとしていることを実際には得ていませんが、db [int(artist)] = ... 'はValueErrorを発生させる必要があります。整数ではありません - あなたがしようとしていることを明確にしてください。 csv-Fileからデータを読み込み、アーティストのすべての絵をリストの要素として関連付けていますか? – Maurice