Skip to content

Instantly share code, notes, and snippets.

@martinholub
Created October 10, 2018 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinholub/8fc7e111f16035de5d8a8c4c92e159fb to your computer and use it in GitHub Desktop.
Save martinholub/8fc7e111f16035de5d8a8c4c92e159fb to your computer and use it in GitHub Desktop.
Utilites for handling lists and filenames
def rm_ext(fname, ext = None):
"""Removes extension from filename"""
if not ext:
ext = re.match(".*\.(.*)$", fname).group(1)
else:
ext = ext.strip(".")
return re.sub("\." + ext + "$","", fname)
def uniq_list(array):
""" Returns unique elements in list in original order
set() does not preserve order. In python 3.5, OrderedDict is implemented in C,
and thus acceptable alternative. For python 3.6+, it can be replaced with regular
dict as it became ordered.
References
https://stackoverflow.com/a/39835527/6877443
"""
return list(OrderedDict.fromkeys(array))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment