Re: Compairing filenames in a list

Discussion in 'Python' started by Arnaud Delobelle, Sep 30, 2012.

  1. On 30 September 2012 02:27, 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)?


    >>> 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
    Arnaud Delobelle, Sep 30, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    734
    Toby Inkster
    Apr 23, 2005
  2. anthonyberet
    Replies:
    5
    Views:
    423
    anthonyberet
    Feb 8, 2005
  3. sonu

    compairing No without Operator

    sonu, Mar 16, 2006, in forum: C Programming
    Replies:
    17
    Views:
    537
    Mark McIntyre
    Mar 18, 2006
  4. =?Utf-8?B?TWljaGFlbA==?=

    Compairing PasswordQuestion Answers

    =?Utf-8?B?TWljaGFlbA==?=, Apr 2, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    326
    =?Utf-8?B?TWljaGFlbA==?=
    Apr 2, 2007
  5. Dave Angel

    Re: Compairing filenames in a list

    Dave Angel, Sep 30, 2012, in forum: Python
    Replies:
    0
    Views:
    235
    Dave Angel
    Sep 30, 2012
Loading...

Share This Page