Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Compairing filenames in a list

30 views
Skip to first unread message

Dave Angel

unread,
Sep 29, 2012, 9:41:03 PM9/29/12
to Kevin Anthony, pytho...@python.org
On 09/29/2012 09:27 PM, Kevin Anthony wrote:
> I have a list of filenames, and i need to find files with the same
> name, different extensions, and split that into tuples. does anyone have
> any suggestions on an easy way to do this that isn't O(n^2)?
>
>

Sure, collect them in a collections.defaultdict Use basename for the
key, and a list of names as the data item.

if that's not good enough, show us your code, and why it doesn't work,
and tell us what version of Python you're using.

--

DaveA

Arnaud Delobelle

unread,
Sep 30, 2012, 6:08:32 PM9/30/12
to Kevin Anthony, pytho...@python.org
On 30 September 2012 02:27, Kevin Anthony <kevin.s...@gmail.com> wrote:
> I have a list of filenames, and i need to find files with the same name,
> different extensions, and split that into tuples. does anyone have any
> suggestions on an easy way to do this that isn't O(n^2)?

>>> import os, itertools
>>> filenames = ["foo.png", "bar.csv", "foo.html", "bar.py"]
>>> dict((key, tuple(val)) for key, val in itertools.groupby(sorted(filenames), lambda f: os.path.splitext(f)[0]))
{'foo': ('foo.html', 'foo.png'), 'bar': ('bar.csv', 'bar.py')}

--
Arnaud
0 new messages