I get a list of values from the following query set,
s_v = SiverifyVerificationSite.objects.filter(pattern_id=int(p_id), if_target=bundle.obj.pattern.if_target).values_list('create_status', flat=True)
print 'svid', s_v
This query set runs for many a times and it returns a list of values. For eg., the o/p for the above print statement on a single occasion of query set run is,
svid [1L, 1L],
svid [6L, 8L],
svid [1L, 6L, 1L, 1L],
svid [1L, 6L, 1L]
The range of values in the list would be from 0L to 8L. I now need to prioritize the values I get in the list. The highest priority in my case is for 8L. So, if 8L exists in the list I need to carry out an operation. The second priority is for 6L. If both 8L and 6L exists in the same list, the priority is for 8L. What is the best way to do this. I tried loop statements but this seems to be a complex sort.
Tried code:
def dehydrate_orc_display(self, bundle):
final_data = {}
p_id = ''.join(x for x in bundle.obj.pdb_pid if x.isdigit())
# print 'vioid', bundle.obj.pdb_id
s_verify = SiverifyVerificationSite.objects.filter(pdb_id=bundle.obj.pdb_id, pattern_id=int(p_id), if_target=bundle.obj.pattern.if_target)
s_v = SiverifyVerificationSite.objects.filter(pattern_id=int(p_id), if_target=bundle.obj.pattern.if_target).values_list('create_status', flat=True)
print 'svid', s_v
print 'fields', s_verify
if 8L in s_v:
final_data['create_status'] = 8
if 6L in s_v:
final_data['create_status'] = 6
if 8L and 6L in s_v:
final_data['create_status'] = 8
return final_data
答案 0 :(得分:1)
Maintian a priority list of values in a sorted fashion. If-Else works in a cascading fashion. So, if the list contains the value at 0th place of the sorted list, that is the highest priority. Else, it cascades to check for other values.
For new values, ex. 9L , you need to make an entry into priority_list and ensure sorting.
priority_list = [8L,7L,6L,5L,4L,3L,2L,1L]
x = raw_input("Enter the new priority. Else press enter to continue? ")
if len(x) > 0:
priority_list.append(x)
priority_list.sort(reverse = True)
if priority_list[0] in s_v:
final_data['create_status'] = prioritylist[0]
elif priority_list[1] in s_v:
final_data['create_status'] = priority_list[1]
elif priority_list[2] in s_v:
final_data['create_status'] = priority_list[2]
return final_data