From 1ecc25b9bf2f17f169f23339a3d7ca797395268e Mon Sep 17 00:00:00 2001 From: Lafrite Date: Wed, 26 Feb 2014 12:39:38 +0100 Subject: [PATCH] add last_elem --- pymath/generic.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pymath/generic.py b/pymath/generic.py index dadc2c5..f1a43ef 100644 --- a/pymath/generic.py +++ b/pymath/generic.py @@ -104,6 +104,34 @@ def first_elem(ll): else: return ll +def last_elem(ll): + """Get the last element in imbricates lists + # TODO: Fonction pourrie mais j'ai pas le temps de faire mieux! |mar. janv. 28 22:32:22 CET 2014 + + :param list: list of lists of lists... + :returns: the last element + + >>> last_elem(1) + 1 + >>> last_elem([1,2]) + 2 + >>> last_elem([["abc"]]) + 'c' + >>> last_elem("abc") + 'c' + >>> last_elem([[[1,2],[3,4]], [5,6]]) + 6 + >>> last_elem([[["ab",2],[3,4]], [5,6]]) + 6 + + """ + if hasattr(ll, '__contains__'): + if len(ll) == 1 and type(ll) == str: + return ll[-1] + else: + return last_elem(ll[-1]) + else: + return ll def expand_list(list_list):