Specific Sort of elements to add in new list Python/Django -
i have :
[ [{ 'position': 1,  'user_id': 2,  'value': 4, 'points': 100}], [{ 'position': 2,  'user_id': 6,  'value': 3, 'points': 88}], [{ 'position': 3,  'user_id': 5,  'value': 2, 'points': 77}], [{ 'position': 4,  'user_id': 7,  'value': 1, 'points': 66}], [{ 'position': 5,  'user_id': 3,  'value': 1, 'points': 9}], [{ 'position': 6,  'user_id': 11, 'value': 0,  'points': 9}], [{ 'position': 7,  'user_id': 1,  'value': 0, 'points': 3}], [{ 'position': 8,  'user_id': 10, 'value': 0,  'points': 3}], [{ 'position': 9,  'user_id': 4,  'value': 0, 'points': 2}], [{ 'position': 10,  'user_id': 8, 'value': 0,  'points': 2}] ]   is organized points. idea choose user_id , generate new list selected 5 users.
example:
user_id=3:
[{ 'position': 3,  'user_id': 5,  'value': 2, 'points': 77}], [{ 'position': 4,  'user_id': 7,  'value': 1, 'points': 66}], [{ 'position': 5,  'user_id': 3,  'value': 1, 'points': 9}], [{ 'position': 6,  'user_id': 11, 'value': 0,  'points': 9}], [{ 'position': 7,  'user_id': 1,  'value': 0, 'points': 3}]   it returns user_id 3 in middle 2 users hight , 2 users lower
user_id=2
[{ 'position': 1,  'user_id': 2,  'value': 4, 'points': 100}], [{ 'position': 2,  'user_id': 6,  'value': 3, 'points': 88}], [{ 'position': 3,  'user_id': 5,  'value': 2, 'points': 77}], [{ 'position': 4,  'user_id': 7,  'value': 1, 'points': 66}], [{ 'position': 5,  'user_id': 3,  'value': 1, 'points': 9}],   as user_id hasn't higher users returns 4 lower users. same logic.
user_id=9:
[{ 'position': 6,  'user_id': 11, 'value': 0,  'points': 9}], [{ 'position': 7,  'user_id': 1,  'value': 0, 'points': 3}], [{ 'position': 8,  'user_id': 10, 'value': 0,  'points': 3}], [{ 'position': 9,  'user_id': 4,  'value': 0, 'points': 2}], [{ 'position': 10,  'user_id': 8, 'value': 0,  'points': 2}]   on user_id=9 have 1 user lower add 3 higher users
if example have 2 users in list, should return 2 users. main rules: if have 5 users or more, return 5 users. if have 4 users, return 4 users
how way it? thanks
this update of my answer your original question.
a = [     [{ 'position': 1,  'user_id': 2,  'value': 4, 'points': 100}],     [{ 'position': 2,  'user_id': 6,  'value': 3, 'points': 88}],     [{ 'position': 3,  'user_id': 5,  'value': 2, 'points': 77}],     [{ 'position': 4,  'user_id': 7,  'value': 1, 'points': 66}],     [{ 'position': 5,  'user_id': 3,  'value': 1, 'points': 9}],     [{ 'position': 6,  'user_id': 11, 'value': 0,  'points': 9}],     [{ 'position': 7,  'user_id': 1,  'value': 0, 'points': 3}],     [{ 'position': 8,  'user_id': 10, 'value': 0,  'points': 3}],     [{ 'position': 9,  'user_id': 4,  'value': 0, 'points': 2}],     [{ 'position': 10,  'user_id': 8, 'value': 0,  'points': 2}] ]  # sort if not sorted # a.sort(key=lambda x: x[0]['position'])  def find_index(l, user_id):     = 0     while l[i][0]['user_id'] != user_id:         += 1     return  def get_subset(l, i):     return l[:(i + 1 + max(2, 4 - i))][-5:]  get_subset(a, find_index(a, 3))      
Comments
Post a Comment