2013-12-08 20 views
1

我想通過流派功能進行排序來處理一些python代碼,但我似乎無法讓它工作。目前,我的排序函數得到一個TypeError,指出它只需要2個參數,並且只有1個參數。我從一個json文件中獲取數據。排序功能不能在Python中工作

這裏是我的課的開頭:

import json 
import operator 
from operator import itemgetter 

class Movielist(): 

    dataFile = "mymoviedata.json" 

    def __init__(self): 
     print "Loading Program..." 
     self.welcome() 

    def loadData(self, dataFile): 
     movieObject = [] 
     json_data=open(dataFile) 

     movieObject = json.load(json_data) 
     json_data.close() 
     return movieObject 

這裏就是我所說的功能

if option == "4": 
    print self.sortByGenre() 

下面是函數:

def sortByGenre(self, FoundMedia): 
    sortedgenrelist = [] 
    sortedgenrelist = sorted(FoundMedia, key = itemgetter('genre')) 
    return sortedgenrelist 
+0

'FoundMedia'通過了哪裏?例如'self.sortByGenre(something_missing_here)' –

+0

停止在Python中使用選項卡。我知道這是一種痛苦,因爲所有編輯器都附帶了選項卡,程序員不應該使用製表符(或任何其他扭曲的間距字符),但是Python尤其不應該使用製表符。 – Phlip

回答

0

的問題是:

print self.sortByGenre() 

只傳遞1個參數(self)。該功能需要2:

def sortByGenre(self, FoundMedia): 
    ... 

你應該通過另一種說法 - 有些論據FoundMedia

+0

你能舉一個例子嗎? – Traz3r

+0

@ user2896569 - 不是。它看起來像'FoundMedia'是你想要分類的任何東西。這就是你應該在那裏通過,但不知道你的代碼或你的目的,我不認爲我可以爲你做的其他事情...... – mgilson