Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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!

Update:

language is python!!

4 Answers

Relevance
  • 2 years 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
    2 years 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
    2 years 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.

  • 2 years ago

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

Still have questions? Get your answers by asking now.