List of paths

andrew cooke andrew at acooke.org
Wed Apr 1 12:35:25 CEST 2009


>>> def filter(values):
...   last = None
...   for value in values:
...     if last is None or not value.startswith(last):
...       yield value
...       last = value
...
>>> for x in filter(['/notebook', ....]):
...   print(x)
...
/notebook
/desktop
/server/hp/proliant

andrew


Nico Grubert wrote:
> Dear Python developers
>
> I have the following (sorted) list.
> ['/notebook',
>   '/notebook/mac',
>   '/notebook/mac/macbook',
>   '/notebook/mac/macbookpro',
>   '/notebook/pc',
>   '/notebook/pc/lenovo',
>   '/notebook/pc/hp',
>   '/notebook/pc/sony',
>   '/desktop',
>   '/desktop/pc/dell',
>   '/desktop/mac/imac',
>   '/server/hp/proliant',
>   '/server/hp/proliant/385',
>   '/server/hp/proliant/585'
> ]
>
> I want to remove all paths x from the list if there is a path y in the
> list which is part of x so y.startswith(x) is true.
>
> The list I want to have is:
> ['/notebook', '/desktop', '/server/hp/proliant']
>
> Any idea how I can do this in Python?
>
> Thanks in advance
> Nico
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list