Hello. I am developing a python program for my application. Python receives multiple messages with different serial codes, for every serial code, I assign it a priority value as following:
The priorities are assigned based on the first 2 characters of its serial number. I want "AN" serial number to always go before "PS" and etc..
Now my item list will contain information about device, quantity, serial and the priority. For example it might look like:
Now, the main task is to sort this list and create an array based on the priority numbers. For my sorted array, I only care about the device name. If more than 1 item is assigned to same priority, then just prioritize random. For the example above, I need to sort it as following:
sorted_list = [device2,device3,device1,device4,device5]
I have written a function :
I have multiple for loops that go through the whole list . It starts from the highest priority and then goes down. I am wondering whether there is a more efficient way to sort it based on the priority values other than running for loop multiple times which seems like a very inefficient way.
The priorities are assigned based on the first 2 characters of its serial number. I want "AN" serial number to always go before "PS" and etc..
Code:
def Assign_priorities(Serial):
#Priority list
# PS - #2
# AN - #1
# PA - always last
if(Serial[0:2] == "AN"):
print("PRIORITY 1")
priority = 1
return priority
elif(Serial[0:2] == "PS"):
print("PRIORITY 2")
priority = 2
return priority
elif(Serial[0:2] == "PA"):
print("PRIORITY 4")
priority = 4
return priority
else:
print("PRIORITY 3")
priority = 3
return priority
[code]
after I retrun the priority, I fill an item list with following data:
[code]
item.append(Item(Device,Quantity,Serial,Priority))
Code:
item[0] = ['device1', '10', CB123122, 3];
item[1] = ['device5, '20', EX000022, 4];
item[2] = ['device3', '30', PS123122, 2];
item[3] = ['device4', '40', CB120022, 3];
item[4] = ['device2', '20', AB000122, 1];
sorted_list = [device2,device3,device1,device4,device5]
I have written a function :
Code:
def sort_picking_order(item_list):
global sorted_list
sorted_list = []
#pick_order = 0
# LOOK FOR HIGHEST PRIORITY
for i in range(len(item_list)):
if(item_list[i].Priority == 1):
print("Highest priority item found")
sorted_list.append(item_list[i].Device)
#pick_order = pick_order + 1
else:
print("Highest priority item is not found in the list")
for i in range(len(item_list)):
#print("Serial=",item_list[i].Serial)
if(item_list[i].Priority == 2):
print(" 2nd Highest priority item found")
sorted_list.append(item_list[i].Device)
#pick_order = pick_order + 1
else:
print("2nd Highest priority item is not found in the list")
for i in range(len(item_list)):
#print("Serial=",item_list[i].Serial)
if(item_list[i].Priority == 3):
print(" 3rd priority item found")
sorted_list.append(item_list[i].Device)
#pick_order = pick_order + 1
else:
print("3rd priority item is not found in the list")
for i in range(len(item_list)):
#print("Serial=",item_list[i].Serial)
if(item_list[i].Priority == 4):
print(" last priority item found")
sorted_list.append(item_list[i].Device)
#pick_order = pick_order + 1
else:
print("last priority item is not found in the list")
print("sorted list",sorted_list)