Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bisect
- original_list = [8, 4, 8, 23, 99]
- sorting_criteria = lambda x: x
- filtering_criteria = lambda x: x < 30
- #initial creation
- sorted_list = [[idx, item] for idx, item in enumerate(original_list) if filtering_criteria(item)]
- sorted_list.sort(key=lambda pair: sorting_criteria(pair[1]))
- #insertion
- to_insert = 15
- if filtering_criteria(to_insert):
- for idx, item in sorted_list:
- if sorting_criteria(item) > sorting_criteria(to_insert):
- break
- else:
- idx = len(sorted_list)
- sorted_list.insert(idx, (len(original_list), to_insert))
- original_list.append(to_insert)
- #updating
- index_to_update = 2
- new_value = 42
- if filtering_criteria(new_value):
- sorted_list[index_to_update][1] = new_value
- original_list[sorted_list[index_to_update][0]] = new_value
- else:
- del sorted_list[index_to_update]
- #deletion
- index_to_delete = 0
- index_in_original_list = sorted_list[index_to_delete][0]
- del sorted_list[index_to_delete]
- del original_list[index_in_original_list]
- sorted_list = [[idx if idx < index_in_original_list else idx-1, item] for idx, item in sorted_list]
- #displaying
- print([item for idx, item in sorted_list])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement