Home>
Purpose

Link content
Continuing from this question, the original question is
masters1 = "Takahashi has beautiful legs"
ans = ['Takahashi is','feet',' and']
I wanted to separate masters1 with the words included in ans and output it as ['','Beautiful','','I'].
['','Beautiful','','','I'] I want to change it like this to solve the question below.

masters1 = ['','beautiful','','','is']
ans1 = ['Takahashi is','Foot',' and']
Store the elements in the list of ans1 in order with'' of masters1
I want to output ['Takahashi is','Beautiful','Foot',' and',']].

What I tried
masters2 = ['','beautiful','','is']
ans1 = ['Saito is','feet',' and']
masters2 = [',' == a for m in masters1 for a in ans1 if m ==',']
print (masters2)
#output
[]


I tried to output using in-list comprehension, but I didn't get any elements.
Please teach someone.

  • Answer # 1

    I think the question is wrong.
    I modified the question as follows and thought about it.

    masters1 = ['','beautiful','','','I'm']
    ans1 = ['Takahashi is','Foot',' and']
    of masters1''''In, store the elements in the list of ans1 in order,
    I want to output ['Takahashi is','Beautiful','Foot','Do','I']].

    masters1 = ['','beautiful','','','is']
    ans1 = ['Takahashi is','Foot',' and']
    it = iter (ans1)
    master2 = [s if s else next (it) for s in masters1]
    print (master2)


    Postscript
    The answer to the added question.

    import re
    masters1 = "Takahashi has beautiful legs"
    ans1 = ['Takahashi is','Foot',' and']
    masters1 = ['' if s in ans1 else s
        for s in re.split ('(' +'|'. join (ans1) +')', masters1) if s]
    it = iter (ans1)
    masters2 = [s if s else next (it) for s in masters1]
    print (masters2)

    It might be easier to write.

    Addendum 2
    It was easier to write.

    import re
    masters1 = "Takahashi has beautiful legs"
    ans1 = ['Takahashi is','Foot',' and']
    masters2 = [s for s in re.split ('(' +'|'. join (ans1) +')', masters1) if s]
    print (masters2)


    Addendum 3

    import re
    masters1 = "Takahashi has beautiful legs"
    ans1 = ['Takahashi is','Foot',' and']
    masters2 = ['' if s in ans1 else s
        for s in re.split ('(' +'|'. join (ans1) +')', masters1) if s]
    print (masters2)


    In the first answer, ['','Beautiful','','','I']
    I showed you how to make ['Takahashi',' Beautiful','Foot',' and',']].

    PostscriptSo, "Takahashi has beautiful legs"
    Convert to ['','Beautiful','','','I'] and convert it
    I showed you how to make ['Takahashi',' Beautiful','Foot',' and',']].

    Addendum 2So, "Takahashi has beautiful legs"
    I showed you how to make ['Takahashi',' Beautiful','Foot',' and',']].

    That is, this method isPostscriptIt was already shown in.

    If you don't understand it, even if you get an answer, you don't try to understand what it means.
    I think it's enough because I just ran it and got the results.

    What is'(' +'|'. join (ans1) +')'?
    What is re.split ('(' +'|'. join (ans1) +')', masters1)?
    What is [s for s in re.split ('(' +'|'. join (ans1) +')', masters1)]?
    What is [s for s in re.split ('(' +'|'. join (ans1) +')', masters1) if s]?
    What does it mean to change the leading s to'' if s in ans1 else s?

    If you don't try to understand all this, you'll continue to be in trouble.

  • Answer # 2

    First of all, it seems that something is wrong, but the elements of masters1 do not contain "','". There are four master1s, "''" (empty string), "'beautiful'", "''" (empty string), and "'is'", and the characters have "'" (quotation mark). Does not include.
    So you can't do what you think.

    Also, in order to synthesize the contents of masters1 and ans1 to make a sentence, it is necessary to have a method that is not simply arranged alternately, and unless it is clarified what kind of rule it is, the general procedure will be Instead, it becomes a unique way of doing things.

    For example

    masters2 = [ans1 [0],masters1 [0],
     ans1 [1],
     ans [2],
     master1 [3]]


    It feels like.

  • Answer # 3

    The elements of masters1 do not have',' and cannot be replaced.
    You can do the following by increasing the'' in masters1 on the assumption that you will replace the'' element in mastaers1.

    masters1 = ['','beautiful','','','is']
    ans1 = ['Takahashi is','Foot',' and']
    i = 0
    masters2 = []
    for m in masters1:
        if m:
            masters2.append (m)
        else: else:
            masters2.append (ans1 [i])
            i + = 1
    print (masters2) # ['Takahashi is','Beautiful','Foot','Foot','I']]


    If you want to output using list comprehension notation, only in Python 3.8 or later environment

    masters1 = ['','beautiful','','','is']
    ans1 = ['Takahashi is','Foot',' and']
    i = -1
    masters2 = [m if m else ans1 [(i: = i + 1)] for m in masters1]
    print (masters2) # ['Takahashi is','Beautiful','Foot','Foot','I']]


    Can be written.