Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import bisect
  2.  
  3. original_list = [8, 4, 8, 23, 99]
  4. sorting_criteria = lambda x: x
  5. filtering_criteria = lambda x: x < 30
  6.  
  7. #initial creation
  8. sorted_list = [[idx, item] for idx, item in enumerate(original_list) if filtering_criteria(item)]
  9. sorted_list.sort(key=lambda pair: sorting_criteria(pair[1]))
  10.  
  11. #insertion
  12. to_insert = 15
  13. if filtering_criteria(to_insert):
  14. for idx, item in sorted_list:
  15. if sorting_criteria(item) > sorting_criteria(to_insert):
  16. break
  17. else:
  18. idx = len(sorted_list)
  19. sorted_list.insert(idx, (len(original_list), to_insert))
  20. original_list.append(to_insert)
  21.  
  22. #updating
  23. index_to_update = 2
  24. new_value = 42
  25. if filtering_criteria(new_value):
  26. sorted_list[index_to_update][1] = new_value
  27. original_list[sorted_list[index_to_update][0]] = new_value
  28. else:
  29. del sorted_list[index_to_update]
  30.  
  31. #deletion
  32. index_to_delete = 0
  33. index_in_original_list = sorted_list[index_to_delete][0]
  34. del sorted_list[index_to_delete]
  35. del original_list[index_in_original_list]
  36. sorted_list = [[idx if idx < index_in_original_list else idx-1, item] for idx, item in sorted_list]
  37.  
  38. #displaying
  39. print([item for idx, item in sorted_list])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement