custom_join function added

It joins elements of a list using a joiner and a final custom joiner
This commit is contained in:
Davte 2019-06-24 16:17:52 +02:00
parent b6f4465880
commit 9fccdcc51c
2 changed files with 15 additions and 1 deletions

View File

@ -7,7 +7,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it"
__credits__ = "Marco Origlia"
__license__ = "GNU General Public License v3.0"
__version__ = "1.5.7"
__version__ = "1.5.9"
__maintainer__ = "Davide Testa"
__contact__ = "t.me/davte"

View File

@ -1468,3 +1468,17 @@ def run_aiohttp_server(app, *args, **kwargs):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
web.run_app(app, *args, **kwargs)
def custom_join(_list, joiner, final=None):
"""Join elements of `_list` using `joiner` (`final` as last joiner)."""
_list = list(map(str, _list))
if final is None:
final = joiner
if len(_list) == 0:
return ''
if len(_list) == 1:
return _list[0]
if len(_list) == 2:
return final.join(_list)
return joiner.join(_list[:-1]) + final + _list[-1]