Home>
Purpose

['Today',' is','Breakfast',' is','Eat','Ta'] and ['Noun','Particle','Noun',' Particle','Particle',' Auxiliary verb I want to create a program that outputs the previous word when there is a particle when there are two lists called'].

What I tried
masters = ['Today',' is','Breakfast',' Eat',' Ta']
part = ['noun','particle','noun','particle','verb','auxiliary verb']
m_with_p = list (zip (masters, part,))


I don't know under what conditions I should write after this.
I would be grateful if you could teach me.

  • Answer # 1

    A list comprehension of the answers that have already been given.

    masters = ['Today',' is','Breakfast',' Eat',' Ta']
    part = ['noun','particle','noun','particle','verb','auxiliary verb']
    ans = [masters [i --1] if i>0 else None for i, v in enumerate (part) if v =='particle']
    print (ans)

    If there is a "particle" at the beginning of part, if it is as it ismasters [-1]Since the element at the end of the list is obtained with, the conditional operator is used to return None.

  • Answer # 2

    I think there are various things, but for example, using enumerate to turn part in a for statement, for an index whose element is a'particle', that index-the first element is brought from masters.
    How to use Python, enumerate: Get list elements and indexes

    * When asking a question, it is better to write as much as you can and ask a question such as "an error occurred" or "the behavior is different from what you expected". Because it can be regarded as a round throw.