coding question?

I am working on a project and am wondering how to access the first word from different sets. What I mean is, say I have something like this:

blue is my fav color

red is my fav color

green is my fav color


yellow is my fav color

purple is my fav color

black is my fav color


pink is my fav color

orange is my fav color

where the dashed lines have some other text (if it helps, the text in between each set is almost identical each time)

I only want the words (in a list) blue, yellow, and pink. I have figured out how to get all the first words using split, but I cant figure out how to only access the first word in each set. anyone have any tips??

thanks in advance!


language is python!!

4 Answers

  • 6 months ago

    basically you want something like this - you need to process in lines

    Array of First Words

    Is_Last_Line_not_favourite = false

    read in first line of text

    while not end of file

    if line contains 'fav colour' and Is_last_line_not_favourite then

    store your first word in your array of words

    last_line_is_favourite = true

    end if

    if last line does not contain 'fav colour'

    last_line_is_favourite = false

    end if

    read the next line

    end of file

  • John
    Lv 7
    6 months ago

    i cannot see why a private project source code like this cannot be shown?

    sets = []

    skip = False

    f = open(filename, 'r')

    for data in f:

    .. if '-' == data[0]:

    .. .. skip = False

    .. .. continue

    .. elif not skip:

    .. .. sets.append(data.split(' ')[0])

    .. .. skip = True

  • EddieJ
    Lv 7
    6 months ago

    Generally, when you say you have a part of the program working then you should show your code.

    But, basically, you just need to set a Boolean variable to True at the start and then every time you detect your separator line, and then to False after processing the first line in a group.

    • EddieJ
      Lv 7
      6 months agoReport

      I made a small change and then, suddenly, my answer became invisible. This is a bug I've seen before. So, I keep moving parts of the answer into comments until it reappears, so, it's a bit jumbled.

  • 6 months ago

    I wish I could help you I just know nothing about computers and coding

Still have questions? Get answers by asking now.