Created
September 16, 2020 17:03
-
-
Save Gaikanomer9/8f89236dd4524ac09f5749cc87ba700f to your computer and use it in GitHub Desktop.
Simple example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# время (O(n1)), память O(n1+2*n2) | |
# Данное решение использует hash таблицу и позволяет быстро определять - находится ли элементов во втором списке | |
# Из минусов - выделение дополнительной памяти для сета второго списка | |
arr1=[1,2,3,4,5] | |
arr2=[3,4,5,6,7] | |
def process_list(arr1, arr2): | |
arr2_s = set(arr2) | |
return list(filter(lambda x: x not in arr2_s, arr1)) | |
process_list(arr1, arr2) | |
# Здесь мы используем удобную форму comprehension для прохода через список + изменяем начальный список | |
# и тем самым не потребляем больше памяти, чем O(1) | |
array = [0, 1, 0, 0, 4, 5, 6, 0, -4] | |
array[:] = (el for el in array if el != 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment