<p>这可能是一个要求,也可能不是,但是如果column1中的元素在textSplitted中出现多次,那么发布的解决方案(当我发布它们时,可能它们现在已更新)将失败,例如:</p>
<pre><code>textSplitted = ['wow','this','is','some','nice','text','yes','indeed','it','is']
column1 = ['this','is','some','text']
output will be:
['wow', 'this', 'is', 'some', 'nice', 'text', 'yes', 'indeed', 'it', 'is']
['-', 'A', 'B', 'C', '-', 'D', '-', '-', '-', '-']
['-', 'Q1', 'Q2', 'Q3', '-', 'Q4', '-', '-', '-', '-']
failing to pick up the repeated 'is'.
</code></pre>
<p>下面修复了该潜在问题:</p>
<pre><code>textSplitted = ['wow','this','is','some','nice','text','yes','indeed','it','is']
column1 = ['this','is','some','text']
column2 = ['A','B','C','D']
column3 = ['Q1','Q2','Q3','Q4',]
a = list(map(lambda w: w if w in column1 else '-', textSplitted))
column2 = list(map(lambda w: w if w=='-' else column2[column1.index(w)], a))
column3 = list(map(lambda w: w if w=='-' else column3[column1.index(w)], a))
print(textSplitted)
print(column2)
print(column3)
['wow', 'this', 'is', 'some', 'nice', 'text', 'yes', 'indeed', 'it', 'is']
['-', 'A', 'B', 'C', '-', 'D', '-', '-', '-', 'B']
['-', 'Q1', 'Q2', 'Q3', '-', 'Q4', '-', '-', '-', 'Q2']
</code></pre>