Question
Hi, I have a list comprehension that is created from dictionary like this below: sensors = {4364:(Math Center,0), 4210:(Science Lab,1), 4250:(Biology Lab,2), 4281:(Finance Department,3), 4245:(Tiled
Hi, I have a list comprehension that is created from dictionary like this below:
sensors = {"4364":("Math Center",0), "4210":("Science Lab",1), "4250":("Biology Lab",2), "4281":("Finance Department",3), "4245":("Tiled Room",4), "In":("Inside",5) } sensor_list = [(r,d,s) for r,(d,s) in sensors.items()]
I then create recursive bubble sort like below:
def recursive_sort(list_to_sort,key=0): sorted = True for i in range(len(list_to_sort)-1): if list_to_sort[i][key] > list_to_sort[i+1][key]: (list_to_sort[i+1],list_to_sort[i]) = (list_to_sort[i],list_to_sort[i+1]) sorted = False if sorted: return list_to_sort else: return recursive_sort(list_to_sort[:len(list_to_sort)-1],key) + list_to_sort[-1:] Then I run print(sensor_list) print(recursive_sort(sensor_list, 0)) print(recursive_sort(sensor_list, 1)) Everything is fine on those 3 above runs. However I want the last run print(sensor_list) returns the original list before sorted. Can you help? Thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started