2013-02-11 33 views
1

所以我有一個模型Education,它附加到UserProfile。這種模式的工作原理如下:User已經去過不同地區的幾所學校。Django和Python的基本匹配

我想要做的是根據用戶去哪裏做一個「分數」。基本上,如果他們去同一所學校,他們得到10分,同樣的城市,5分,相同的狀態,2分,等等。

我已經做了一些功能來嘗試這個,但我失敗了。有什麼建議?

info = {} 
def edu_info(user1): 
    user_1_cities = [] 
    user_1_schools = [] 
    user_1_state = [] 
    first_one = Education.objects.filter(owner=user1) 
    for i in first_one: 
     user_1_cities.append(str(i.city)) 
     user_1_schools.append(str(i.school)) 
     user_1_state.append(str(i.state)) 
     info[str(i.owner.username)] = {} 
     info[str(i.owner.username)]['cities'] = user_1_cities 
     info[str(i.owner.username)]['schools'] = user_1_schools 
     info[str(i.owner.username)]['state'] = user_1_state 
    return info 

def check_match(user1, user2): 
    match_score = {}  
    first_info = edu_info(user1) 
    dict = edu_info(user2) 
    for item in dict: 
     cities = dict[item]['cities'] 
     #user2 = item 
     #print cities 
     for city in cities: 
      if city in first_info['jmitchel3']['cities']: 
       match_score['user'] = 'jmitchel3' 
       match_score['user2'] = str(user2.user.username) 
       match_score['city'] = city 
       print "here! " + str(city) 
      else: 
       print "not here! " + str(city) 

    return match_score 




check_match(j,t) 

回答

1

這樣的事情呢?

開始收集關於單個用戶的信息,並返回一個字典對於用戶的功能:

def edu_info(user1): 
    user_1_cities = [] 
    user_1_schools = [] 
    user_1_state = [] 
    first_one = Education.objects.filter(owner=user1) 
    for i in first_one: 
     user_1_cities.append(str(i.city)) 
     user_1_schools.append(str(i.school)) 
     user_1_state.append(str(i.state)) 
    info = {} 
    info['cities'] = user_1_cities 
    info['schools'] = user_1_schools 
    info['states'] = user_1_state 
    return info 

然後,有一個單獨的函數調用的信息,每兩個用戶收集功能一次,並用這些信息計算點數。

def check_match(user_1, user_2) 
    info_1 = edu_info(user_1) 
    info_2 = edu_info(user_2) 
    school_points = similarity_points(info_1["schools"], info_2["schools"], 10) 
    city_points = similarity_points(info_1["cities"], info_2["cities"], 5) 
    state_points = similarity_points(info_1["states"], info_2["states"], 2) 
    return school_points + city_points + state_points 

check_match函數的肉被卸載到它自己的幫助函數中。該函數查找兩個屬性列表的交集,並將共享屬性數乘以某個點值。它通過將屬性列表轉換爲集合然後使用set intersection operator來完成此操作。

所以,similarity_points(["MN","OR","PA", "NJ"],["AZ","NJ","PA"], 2)會覺得兩場比賽(新澤西州和賓夕法尼亞州),因此返回4.

def similarity_points(attr_1, attr_2, points) 
    """Award a number of points for each shared attribute. 

    attr_1 and attr_2 should be lists to compare. 
    """ 
    number_shared = len(set(attr_1) & set(attr_2)) 
    return number_shared * points 

然後,您可以撥打上面的代碼,如:

wilduck_jmitchel3_points = check_match("Wilduck", "jmitchel3") 
+0

好傢伙!棒極了。謝謝你的幫助! (也很高興知道我是在正確的軌道上!) – jmitchel3 2013-02-11 23:41:07

+0

Wilduck,有沒有一種方法來遍歷django模型中的所有字段並分配給字典?因此,像'user_1_schools.append(str(i.major))'這樣的東西會被添加到組合中,而不會對其進行硬編碼?或者這樣做沒有意義,因爲我也必須指定點值? – jmitchel3 2013-02-11 23:46:37

+0

@ jmitchel3我對Django並不是很熟悉,但是您可以在QuerySet的'values'方法上做些事情,請參閱:https://docs.djangoproject.com/en/dev/ref/models/querysets /#值。但是,在這種情況下,如果你沒有太多的領域,手工操作可能會更加清晰。 – Wilduck 2013-02-12 00:08:00