Newbie Question

jmdeschamps at gmail.com jmdeschamps at gmail.com
Mon Jun 19 23:10:11 EDT 2006


Saint Malo wrote:
> BTW my program isn't about red blue yellow etc.  I'm just using it as
> an example.  I guess i didn't asked the question correctly or am not
> expressing myself correctly.   Let me try one more.
>
> Ok.
>
> Contents of text file follow:
>
> red blue purble
> yellow blue green
>
>
>
> Now lets imagine i wrote some code to look for the word red.  Once the
> program has found red i want it to print something like the following
> line.
>
>
> If you add red and blue you obtain the color purple...
>
>
> How do i turn each color in the line into a separate string? Now is
> that possible?
>
... Ok myself!

Let's start at the beginning
1- a text string is text, not colors, right?
2- how does a computer know that you are looking for a color? Because
you give it a label say 'red' (which is a text string) and it looks for
that pattern of caracters in the string you are giving it as a target.
3- so you already HAVE the string you want, no?

So, what I guess you question really is might look like the following:
if I find colors words in a line,
find what color is produced by the combination of all those I found in
the line

IF this is the case then what you may want is something like this:
mycolors=["red", "green"]
for line in file:
    listofwords = line.split(" ") # is a space is seperating the words
in the actual line
    activecolors=[] # an empty list to use as local container
    for word in listofwords:
        if word in mycolors:
            activecolor.append(word) # keep a note of what color was
found in this line
    dosomething(activecolors)

Before I continue with that line of answer, does this seem closer to
what you care to do?

jm




More information about the Python-list mailing list