473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bit of List replacing trouble (newbie)


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-re...p17397379.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #1
2 1906
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

Jun 27 '08 #2
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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
0
by: Brian van den Broek | last post by:
Hi all, There have been a few posts over the last month or so expressing a bit of exasperation with the "rising tide of newbie's". (Or, more accurately, the rising tide of questions from...
11
by: lucky | last post by:
hi, i got file which contains "----------------" in a line. the line only contains this data as a saperation. using regular expression i want to i detify the line contains that data and replace...
4
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option...
9
by: natwong | last post by:
Hi All, I'm a newbie in terms of Access and some of its functionality . I've been stuck on this problem for a couple days, even after searching the Web, etc. Currently I have five combo boxes...
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
6
by: kimiraikkonen | last post by:
Hello, I've been working on a project and i wonder how to get list of the LAN computer names in a tree list which are involved under the same workgroup name? What can be the code be? Very...
5
by: Roy Smith | last post by:
Be kind to me, I'm a CSS newbie... I've been playing with drupal, building a web site (hyc-test.org). I started with the "sky" theme, but didn't like the way it rendered list items in menus. ...
7
by: Qbert16 | last post by:
I'm having a lot of trouble with the following task for one of my assignments in python. Basically I need to take the user input and replace any words in the input and replace them with words from a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.