searching for matches within a word list

Discussion in 'Python' started by Rajarshi Guha, Jul 29, 2003.

  1. Hi,
    I have a large list of words (each on its own line)
    and I would like to find words that contain a specific string.

    I have been trying to use a regexp but I cant see how I can find the
    word that contained the regex pattern - all I get is the pattern itself
    if I use match or findall.

    I know that I could just go through the list line by line and see if the
    regex matches or not - but that method seems horifically inefficient.

    Does anybody have any suggestions as to how I could solve this
    efficiently?

    Thanks,
    Rajarshi
     
    Rajarshi Guha, Jul 29, 2003
    #1
    1. Advertising

  2. Rajarshi Guha wrote:
    > Hi,
    > I have a large list of words (each on its own line)
    > and I would like to find words that contain a specific string.

    How bad is (for 2.3)?
    result = []
    apnd = result.append
    source = file('data', 'U')
    for line in source:
    if specific in line:
    apnd(line)
    source.close()
    # result is the list you want

    -Scott David Daniels
     
    Scott David Daniels, Jul 30, 2003
    #2
    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. Replies:
    6
    Views:
    483
    Richard Heathfield
    Dec 3, 2005
  2. Abe
    Replies:
    2
    Views:
    177
  3. jackster the jackle

    Searching Text for Multiple Matches

    jackster the jackle, Nov 30, 2009, in forum: Ruby
    Replies:
    3
    Views:
    120
    Robert Klemme
    Dec 2, 2009
  4. jhu
    Replies:
    6
    Views:
    123
    Dave Weaver
    Nov 26, 2007
  5. Rudra Banerjee

    get the full string when a word matches

    Rudra Banerjee, Sep 18, 2012, in forum: C Programming
    Replies:
    6
    Views:
    348
    Ben Bacarisse
    Sep 19, 2012
Loading...

Share This Page