cengal.data_manipulation.performant_list_operations.versions.v_0.remove_items_from_list
1#!/usr/bin/env python 2# coding=utf-8 3 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space> 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18__all__ = ['remove_items_from_list', 'is_need_to_convert_list_to_set_and_back_to_list', 'is_need_to_convert_list_to_set', 'is_need_to_convert_removable_list_to_set'] 19 20from typing import List, Set, Union 21 22""" 23Module Docstring 24Docstrings: http://www.python.org/dev/peps/pep-0257/ 25""" 26 27__author__ = "ButenkoMS <gtalk@butenkoms.space>" 28__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>" 29__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ] 30__license__ = "Apache License, Version 2.0" 31__version__ = "4.4.1" 32__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>" 33__email__ = "gtalk@butenkoms.space" 34# __status__ = "Prototype" 35__status__ = "Development" 36# __status__ = "Production" 37 38 39def is_need_to_convert_list_to_set_and_back_to_list(list_len: int, iterations_num: int) -> bool: 40 convert_list_to_set = False 41 if list_len < 100: 42 convert_list_to_set = False 43 elif list_len <= 1000: 44 if iterations_num < 4: 45 convert_list_to_set = False 46 else: 47 convert_list_to_set = True 48 elif list_len <= 10000: 49 if iterations_num < 2: 50 convert_list_to_set = False 51 else: 52 convert_list_to_set = True 53 elif list_len <= 100000: 54 if iterations_num < 3: 55 convert_list_to_set = False 56 else: 57 convert_list_to_set = True 58 elif list_len <= 1000000: 59 if iterations_num < 4: 60 convert_list_to_set = False 61 else: 62 convert_list_to_set = True 63 elif list_len <= 10000000: 64 if iterations_num < 5: 65 convert_list_to_set = False 66 else: 67 convert_list_to_set = True 68 elif list_len <= 100000000: 69 if iterations_num < 6: 70 convert_list_to_set = False 71 else: 72 convert_list_to_set = True 73 else: 74 if iterations_num < 7: 75 convert_list_to_set = False 76 else: 77 convert_list_to_set = True 78 79 return convert_list_to_set 80 81 82def is_need_to_convert_list_to_set(list_len: int, iterations_num: int) -> bool: 83 convert_list_to_set = False 84 if list_len < 100: 85 convert_list_to_set = False 86 elif list_len <= 1000: 87 if iterations_num < 3: 88 convert_list_to_set = False 89 else: 90 convert_list_to_set = True 91 elif list_len <= 10000: 92 if iterations_num < 2: 93 convert_list_to_set = False 94 else: 95 convert_list_to_set = True 96 elif list_len <= 100000: 97 if iterations_num < 3: 98 convert_list_to_set = False 99 else: 100 convert_list_to_set = True 101 elif list_len <= 1000000: 102 if iterations_num < 3: 103 convert_list_to_set = False 104 else: 105 convert_list_to_set = True 106 elif list_len <= 10000000: 107 if iterations_num < 4: 108 convert_list_to_set = False 109 else: 110 convert_list_to_set = True 111 elif list_len <= 100000000: 112 if iterations_num < 4: 113 convert_list_to_set = False 114 else: 115 convert_list_to_set = True 116 else: 117 if iterations_num < 4: 118 convert_list_to_set = False 119 else: 120 convert_list_to_set = True 121 122 return convert_list_to_set 123 124 125def is_need_to_convert_removable_list_to_set(source_list_len: int, removable_list_len: int) -> bool: 126 convert_removable_list_to_set = False 127 if source_list_len == 1: 128 if removable_list_len < 21: 129 convert_removable_list_to_set = False 130 else: 131 convert_removable_list_to_set = True 132 elif source_list_len == 2: 133 if removable_list_len < 20: 134 convert_removable_list_to_set = False 135 else: 136 convert_removable_list_to_set = True 137 elif source_list_len == 3: 138 if removable_list_len < 25: 139 convert_removable_list_to_set = False 140 else: 141 convert_removable_list_to_set = True 142 elif source_list_len == 4: 143 if removable_list_len < 30: 144 convert_removable_list_to_set = False 145 else: 146 convert_removable_list_to_set = True 147 elif source_list_len == 5: 148 if removable_list_len < 31: 149 convert_removable_list_to_set = False 150 else: 151 convert_removable_list_to_set = True 152 elif source_list_len == 6: 153 if removable_list_len < 35: 154 convert_removable_list_to_set = False 155 else: 156 convert_removable_list_to_set = True 157 elif source_list_len == 7: 158 if removable_list_len < 42: 159 convert_removable_list_to_set = False 160 else: 161 convert_removable_list_to_set = True 162 elif source_list_len == 8: 163 if removable_list_len < 47: 164 convert_removable_list_to_set = False 165 else: 166 convert_removable_list_to_set = True 167 elif source_list_len == 9: 168 if removable_list_len < 52: 169 convert_removable_list_to_set = False 170 else: 171 convert_removable_list_to_set = True 172 elif source_list_len == 10: 173 if removable_list_len < 60: 174 convert_removable_list_to_set = False 175 else: 176 convert_removable_list_to_set = True 177 elif source_list_len == 11: 178 if removable_list_len < 70: 179 convert_removable_list_to_set = False 180 else: 181 convert_removable_list_to_set = True 182 elif source_list_len == 12: 183 if removable_list_len < 81: 184 convert_removable_list_to_set = False 185 else: 186 convert_removable_list_to_set = True 187 elif source_list_len == 13: 188 if removable_list_len < 85: 189 convert_removable_list_to_set = False 190 else: 191 convert_removable_list_to_set = True 192 elif source_list_len == 14: 193 if removable_list_len < 90: 194 convert_removable_list_to_set = False 195 else: 196 convert_removable_list_to_set = True 197 else: 198 convert_removable_list_to_set = False 199 200 return convert_removable_list_to_set 201 202 203def remove_items_from_list(source: Union[List, Set], removable: Union[List, Set], preserve_source_order: bool = False, remove_duplicates: bool = False) -> List: 204 """[Will choose most fast algorithm according to given datasets lengths] 205 206 Args: 207 source (Union[List, Set]): [Input data set. Original external value may be modified inside this function. Use `copy.copy(source)`, `list(source)` or `set(source)` when you need to prevent modification of the original data set] 208 removable (Union[List, Set]): [list of items which should be removed out from source data set] 209 preserve_source_order (bool, optional): [Will always preserve source's data order on True (not a fastest choose in some cases)]. Defaults to False. 210 remove_duplicates (bool, optional): [Will always remove duplicates out from source's data set on True (not a fastest choose in some cases)]. Defaults to False. 211 212 Returns: 213 List: [resulting data set] 214 """ 215 convert_removable_list_to_set = preserve_source_order or is_need_to_convert_removable_list_to_set(len(source), len(removable)) 216 217 if convert_removable_list_to_set: 218 if remove_duplicates: 219 new_source = set() 220 convert_list_to_set = is_need_to_convert_list_to_set_and_back_to_list(len(removable), len(source)) 221 if convert_list_to_set: 222 if not isinstance(removable, set): 223 removable = set(removable) 224 225 for item in source: 226 if item in removable: 227 continue 228 new_source.add(item) 229 230 return list(new_source) 231 else: 232 new_source = list() 233 convert_list_to_set = is_need_to_convert_list_to_set_and_back_to_list(len(removable), len(source)) 234 if convert_list_to_set: 235 if not isinstance(removable, set): 236 removable = set(removable) 237 238 for item in source: 239 if item in removable: 240 continue 241 new_source.append(item) 242 243 return new_source 244 245 convert_source_list_to_set = remove_duplicates or is_need_to_convert_list_to_set_and_back_to_list(len(source), len(removable)) 246 if convert_source_list_to_set: 247 if not isinstance(source, set): 248 source = set(source) 249 250 for removed in removable: 251 if removed in source: 252 source.remove(removed) 253 254 if convert_source_list_to_set: 255 return list(source) 256 else: 257 return source
204def remove_items_from_list(source: Union[List, Set], removable: Union[List, Set], preserve_source_order: bool = False, remove_duplicates: bool = False) -> List: 205 """[Will choose most fast algorithm according to given datasets lengths] 206 207 Args: 208 source (Union[List, Set]): [Input data set. Original external value may be modified inside this function. Use `copy.copy(source)`, `list(source)` or `set(source)` when you need to prevent modification of the original data set] 209 removable (Union[List, Set]): [list of items which should be removed out from source data set] 210 preserve_source_order (bool, optional): [Will always preserve source's data order on True (not a fastest choose in some cases)]. Defaults to False. 211 remove_duplicates (bool, optional): [Will always remove duplicates out from source's data set on True (not a fastest choose in some cases)]. Defaults to False. 212 213 Returns: 214 List: [resulting data set] 215 """ 216 convert_removable_list_to_set = preserve_source_order or is_need_to_convert_removable_list_to_set(len(source), len(removable)) 217 218 if convert_removable_list_to_set: 219 if remove_duplicates: 220 new_source = set() 221 convert_list_to_set = is_need_to_convert_list_to_set_and_back_to_list(len(removable), len(source)) 222 if convert_list_to_set: 223 if not isinstance(removable, set): 224 removable = set(removable) 225 226 for item in source: 227 if item in removable: 228 continue 229 new_source.add(item) 230 231 return list(new_source) 232 else: 233 new_source = list() 234 convert_list_to_set = is_need_to_convert_list_to_set_and_back_to_list(len(removable), len(source)) 235 if convert_list_to_set: 236 if not isinstance(removable, set): 237 removable = set(removable) 238 239 for item in source: 240 if item in removable: 241 continue 242 new_source.append(item) 243 244 return new_source 245 246 convert_source_list_to_set = remove_duplicates or is_need_to_convert_list_to_set_and_back_to_list(len(source), len(removable)) 247 if convert_source_list_to_set: 248 if not isinstance(source, set): 249 source = set(source) 250 251 for removed in removable: 252 if removed in source: 253 source.remove(removed) 254 255 if convert_source_list_to_set: 256 return list(source) 257 else: 258 return source
[Will choose most fast algorithm according to given datasets lengths]
Args:
source (Union[List, Set]): [Input data set. Original external value may be modified inside this function. Use copy.copy(source)
, list(source)
or set(source)
when you need to prevent modification of the original data set]
removable (Union[List, Set]): [list of items which should be removed out from source data set]
preserve_source_order (bool, optional): [Will always preserve source's data order on True (not a fastest choose in some cases)]. Defaults to False.
remove_duplicates (bool, optional): [Will always remove duplicates out from source's data set on True (not a fastest choose in some cases)]. Defaults to False.
Returns: List: [resulting data set]
40def is_need_to_convert_list_to_set_and_back_to_list(list_len: int, iterations_num: int) -> bool: 41 convert_list_to_set = False 42 if list_len < 100: 43 convert_list_to_set = False 44 elif list_len <= 1000: 45 if iterations_num < 4: 46 convert_list_to_set = False 47 else: 48 convert_list_to_set = True 49 elif list_len <= 10000: 50 if iterations_num < 2: 51 convert_list_to_set = False 52 else: 53 convert_list_to_set = True 54 elif list_len <= 100000: 55 if iterations_num < 3: 56 convert_list_to_set = False 57 else: 58 convert_list_to_set = True 59 elif list_len <= 1000000: 60 if iterations_num < 4: 61 convert_list_to_set = False 62 else: 63 convert_list_to_set = True 64 elif list_len <= 10000000: 65 if iterations_num < 5: 66 convert_list_to_set = False 67 else: 68 convert_list_to_set = True 69 elif list_len <= 100000000: 70 if iterations_num < 6: 71 convert_list_to_set = False 72 else: 73 convert_list_to_set = True 74 else: 75 if iterations_num < 7: 76 convert_list_to_set = False 77 else: 78 convert_list_to_set = True 79 80 return convert_list_to_set
83def is_need_to_convert_list_to_set(list_len: int, iterations_num: int) -> bool: 84 convert_list_to_set = False 85 if list_len < 100: 86 convert_list_to_set = False 87 elif list_len <= 1000: 88 if iterations_num < 3: 89 convert_list_to_set = False 90 else: 91 convert_list_to_set = True 92 elif list_len <= 10000: 93 if iterations_num < 2: 94 convert_list_to_set = False 95 else: 96 convert_list_to_set = True 97 elif list_len <= 100000: 98 if iterations_num < 3: 99 convert_list_to_set = False 100 else: 101 convert_list_to_set = True 102 elif list_len <= 1000000: 103 if iterations_num < 3: 104 convert_list_to_set = False 105 else: 106 convert_list_to_set = True 107 elif list_len <= 10000000: 108 if iterations_num < 4: 109 convert_list_to_set = False 110 else: 111 convert_list_to_set = True 112 elif list_len <= 100000000: 113 if iterations_num < 4: 114 convert_list_to_set = False 115 else: 116 convert_list_to_set = True 117 else: 118 if iterations_num < 4: 119 convert_list_to_set = False 120 else: 121 convert_list_to_set = True 122 123 return convert_list_to_set
126def is_need_to_convert_removable_list_to_set(source_list_len: int, removable_list_len: int) -> bool: 127 convert_removable_list_to_set = False 128 if source_list_len == 1: 129 if removable_list_len < 21: 130 convert_removable_list_to_set = False 131 else: 132 convert_removable_list_to_set = True 133 elif source_list_len == 2: 134 if removable_list_len < 20: 135 convert_removable_list_to_set = False 136 else: 137 convert_removable_list_to_set = True 138 elif source_list_len == 3: 139 if removable_list_len < 25: 140 convert_removable_list_to_set = False 141 else: 142 convert_removable_list_to_set = True 143 elif source_list_len == 4: 144 if removable_list_len < 30: 145 convert_removable_list_to_set = False 146 else: 147 convert_removable_list_to_set = True 148 elif source_list_len == 5: 149 if removable_list_len < 31: 150 convert_removable_list_to_set = False 151 else: 152 convert_removable_list_to_set = True 153 elif source_list_len == 6: 154 if removable_list_len < 35: 155 convert_removable_list_to_set = False 156 else: 157 convert_removable_list_to_set = True 158 elif source_list_len == 7: 159 if removable_list_len < 42: 160 convert_removable_list_to_set = False 161 else: 162 convert_removable_list_to_set = True 163 elif source_list_len == 8: 164 if removable_list_len < 47: 165 convert_removable_list_to_set = False 166 else: 167 convert_removable_list_to_set = True 168 elif source_list_len == 9: 169 if removable_list_len < 52: 170 convert_removable_list_to_set = False 171 else: 172 convert_removable_list_to_set = True 173 elif source_list_len == 10: 174 if removable_list_len < 60: 175 convert_removable_list_to_set = False 176 else: 177 convert_removable_list_to_set = True 178 elif source_list_len == 11: 179 if removable_list_len < 70: 180 convert_removable_list_to_set = False 181 else: 182 convert_removable_list_to_set = True 183 elif source_list_len == 12: 184 if removable_list_len < 81: 185 convert_removable_list_to_set = False 186 else: 187 convert_removable_list_to_set = True 188 elif source_list_len == 13: 189 if removable_list_len < 85: 190 convert_removable_list_to_set = False 191 else: 192 convert_removable_list_to_set = True 193 elif source_list_len == 14: 194 if removable_list_len < 90: 195 convert_removable_list_to_set = False 196 else: 197 convert_removable_list_to_set = True 198 else: 199 convert_removable_list_to_set = False 200 201 return convert_removable_list_to_set