Bit of List replacing trouble (newbie)

Discussion in 'Python' started by Zethex, May 22, 2008.

  1. Zethex

    Zethex Guest

    At the moment i'm doing a piece of work for school and I'm stuck at the
    moment.

    I have a list of words, for example:

    Sentence = ['A', 'dog', 'walked', 'across', 'the', 'street']

    I have another list which I need to use to replace certain words, and its in
    the form of:

    synonyms = [
    [canine, [dog, puppy, bulldog]],
    [ road, [street, avenue, court]]
    ]
    What the procedure must do is replace dog with canine, and street with road.
    Therefore if a word is in the sentence and appears on the right side of the
    list entry, replace it with the left entry.

    I can't seem to find a help file with what I'm after. I'm just wondering if
    anyone can help me on the right track on how to start this procedure, maybe
    not an answer but just a little help on how to get started as I'm complete
    stuck right now.
    --
    View this message in context: http://www.nabble.com/Bit-of-List-replacing-trouble-(newbie)-tp17397379p17397379.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    Zethex, May 22, 2008
    #1
    1. Advertising

  2. Zethex

    Peter Otten Guest

    Zethex wrote:

    > At the moment i'm doing a piece of work for school and I'm stuck at the
    > moment.
    >
    > I have a list of words, for example:
    >
    > Sentence = ['A', 'dog', 'walked', 'across', 'the', 'street']
    >
    > I have another list which I need to use to replace certain words, and its
    > in the form of:
    >
    > synonyms = [
    > [canine, [dog, puppy, bulldog]],
    > [ road, [street, avenue, court]]
    > ]
    > What the procedure must do is replace dog with canine, and street with
    > road. Therefore if a word is in the sentence and appears on the right side
    > of the list entry, replace it with the left entry.
    >
    > I can't seem to find a help file with what I'm after. I'm just wondering
    > if anyone can help me on the right track on how to start this procedure,
    > maybe not an answer but just a little help on how to get started as I'm
    > complete stuck right now.


    See if you can put the synonyms in a dict:

    syndict = {"dog": "canine", "puppy": "canine", ..., "court": "road"}

    The code to achieve that should consist of two nested loops.
    You can then look up a word easily with

    word = syndict.get(word, word)

    Put that into another loop iterating over the words of the original sentence
    and build the new sentence by appending to a fresh list.

    Peter
     
    Peter Otten, May 22, 2008
    #2
    1. Advertising

  3. Zethex a écrit :
    > At the moment i'm doing a piece of work for school and I'm stuck at the
    > moment.
    >
    > I have a list of words, for example:
    >
    > Sentence = ['A', 'dog', 'walked', 'across', 'the', 'street']


    <ot>
    Naming convention : variable names are all_lower, so s/Sentence/sentence/
    </ot>

    >
    > I have another list which I need to use to replace certain words, and its in
    > the form of:
    >
    > synonyms = [
    > [canine, [dog, puppy, bulldog]],
    > [ road, [street, avenue, court]]
    > ]


    NB : I assume this is :

    synonyms = [
    ['canine', ['dog', 'puppy', 'bulldog']],
    [ 'road', ['street', 'avenue', 'court']],
    ]

    and FWIW, I'd use a list of tuples, not a list of lists:

    synonyms = [
    ('canine', ['dog', 'puppy', 'bulldog']),
    ('road', ['street', 'avenue', 'court']),
    ]

    or even better a dict:

    synonyms = {
    'canine' : ['dog', 'puppy', 'bulldog'],
    'road' : ['street', 'avenue', 'court'],
    }


    > What the procedure must do is replace dog with canine, and street with road.
    > Therefore if a word is in the sentence and appears on the right side of the
    > list entry, replace it with the left entry.
    >
    > I can't seem to find a help file with what I'm after.


    This is probably not something that you'll find in any 'help file'. As
    for almost everything algorithmic, the key is to have the appropriate
    data structure. In this case, the appropriate data structure is
    obviously a reversed index of the synonyms dict (assuming there is no
    duplicate word in the synonyms lists):

    reversed_synomyms = dict()
    for key, values in synonyms.items():
    for word in values:
    reversed_synonyms[word] = key

    print reversed_synonyms

    => {'court': 'road', 'bulldog': 'canine', 'dog': 'canine', 'street':
    'road', 'puppy': 'canine', 'avenue': 'road'}


    From then, the replacement procedure is quite obvious:

    solution = [reversed_synonyms.get(word, word) for word in sentence]


    print solution
    => ['A', 'canine', 'walked', 'across', 'the', 'road']

    HTH
     
    Bruno Desthuilliers, May 22, 2008
    #3
    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:
    3
    Views:
    1,855
    Timothy Bendfelt
    Jan 19, 2007
  2. Replies:
    9
    Views:
    1,049
    Juha Nieminen
    Aug 22, 2007
  3. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    314
    Chris Hohmann
    Apr 11, 2005
  4. Paul
    Replies:
    0
    Views:
    96
  5. Jeff.M
    Replies:
    6
    Views:
    204
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page