Surrerstry.pl

Entries Projects About

 Handy lists operations

22 October 2017

This time just two short sources to making two things.
First for checking if one list contains another list, having regards to order.
Because in Python this syntax: " [2,3] in [1,2,3,4] " doesn't give me True I made for it small function.
I know it could be done a lot easier by for example joining lists and checking if string contains string, for instance: " '23' in '1234' ", but my way need no additional data to create, and everything happens in fly.

Doctest from script:

def if_list_in_list(base_string, sub_string):
  """
  >>> if_list_in_list([1,2,3, 1,3,7, 8,9,0],[1,3,7])
  True
  >>> if_list_in_list([1,2,3,4,5], [5,4,3])
  False
  >>> if_list_in_list([1,2,3,4], [1,2])
  True
  """


Second script is about removing list from list, it's very easy but I put it in here because I use it a lot, so I want to have it accessible to fast copy :)

Doctest from second script:

def remove_list_from_list(base_list, sub_list):
    """
    >>> remove_list_from_list([1,2,3,4], [1,2])
    [3, 4]
    >>> remove_list_from_list([1,2,3,4], [])
    [1, 2, 3, 4]
    >>> remove_list_from_list([], [])
    []
    >>> remove_list_from_list([], [1,2])
    []
    >>> remove_list_from_list([1], [1,2])
    []
    """


Source code:
Github If list contains list
Github Remove list from list
Onsite Handy_lists.zip


Posts



Miscellaneous


Surrerstry.pl © 2014-2017-2018