我想創建像facebook一樣的通知。一切正常,但我有重複。例如,action = like,url = post/1我想要獲取狀態爲1的所有通知 - 未讀,並消除action和url相同的重複。在消除通知重複
if n_dup[i]['url'] == n_dup[j]['url'] and n_dup[i]['action'] == n_dup[j]
def recieve_notification(request):
t = loader.get_template('notifications.html')
nots = Notification.objects.filter(recipent=request.user, status=1, pub_date__gte=datetime.datetime.now()-datetime.timedelta(days=3))
n_dup = [] #list of notifications with duplicates
for n in nots:
n_dup.append({'id':n.id, 'url':n.url, 'action':n.action})
i = len(n_dup)-1
j = len(n_dup)-1
while j>=0:
while i>=0:
if n_dup[i]['url'] == n_dup[j]['url'] and n_dup[i]['action'] == n_dup[j]['action'] and i is not j:
del n_dup[i]
i-=1
j-=1
out_n = []
for n in n_dup:
n_id = n['id']
out_n.append(Notification.objects.get(id=n_id))
c = RequestContext(request, {'notifications':out_n, 'notifications_count':len(out_n)})
return HttpResponse(t.render(c))`
也許你是在更好地瞭解「列表索引超出範圍」編寫這一切的東西:你可以找到下面的代碼我有這樣的錯誤:
錯誤?
awsome,thx夥計。 – user1403568 2012-07-09 18:51:54