在Python中,如何检测一个列表中的多个项目,这些项目由一个相当的列表分隔开?

在Python中,如何检测一个列表中的多个项目,这些项目由一个相当的列表分隔开?,python,string,python-3.x,list,Python,String,Python 3.x,List,我正在用python版本3进行编码,我得到了一个带有肯定“words”的列表,但有些项目有空格: posWords = ['beautiful', 'love', 'happy day', 'enjoy', 'smelling flowers'] 然而,我需要分析的关于正面词语的文本中没有任何空格: wordList = ['I', 'enjoy', 'smelling', 'flowers', 'on', 'a', 'happy', 'day'] 我想迭代单词列表,当算法看到同样在pos

我正在用python版本3进行编码,我得到了一个带有肯定“words”的
列表,但有些项目有空格:

posWords = ['beautiful', 'love', 'happy day', 'enjoy', 'smelling flowers']
然而,我需要分析的关于正面词语的文本中没有任何空格:

wordList = ['I', 'enjoy', 'smelling', 'flowers', 'on', 'a', 'happy', 'day'] 
我想迭代
单词列表
,当算法看到同样在
posWords
中但合并的单词时(例如
'happy day'
),删除
单词列表
中相应的单词(
'happy',day'
),并在
单词列表
中添加合并版本

因此,最后,
单词列表必须如下所示:

wordList = ['I', 'enjoy', 'smelling flowers', 'on', 'a', 'happy day']
重大更新:

因为我答应过你们,让你们不断更新,这是我的代码到目前为止。这有点棘手,因为在我的积极词和消极词列表中,短语最多包含三个词。所以我需要弄清楚如何处理这个问题。我意识到(也是因为你们给了我答案,再次感谢!)我必须从文本中所有需要分析的单词中列出一个字符串项目,其中包括3、2或1个单词,这样我就可以检查这些项目是否也出现在我的肯定词和否定词列表中。这是到目前为止我的代码。它有点笨重,有很多复制粘贴。。。我正计划解决这个问题,但我很累,周末就要开始了,所以请不要讨厌!(欢迎提供提示)


您可以这样做:

In [711]: s = ''.join(posWords)

In [712]: s
Out[712]: 'beautifullovehappy dayenjoysmelling flowers'

In [672]: n = []

In [673]: for i in wordList:
     ...:     if i in s:
     ...:         n.append(i)
     ...: 

In [713]: n
Out[713]: ['enjoy', 'smelling', 'flowers', 'a', 'happy', 'day']

In [740]: for c, i in enumerate(n):
     ...:     if c+1 < len(n):
     ...:         word = n[c] + ' ' + n[c+1]
     ...:         if word in posWords:
     ...:             ix1 = wordList.index(n[c])
     ...:             del wordList[ix1: ix1+2]
     ...:             wordList.insert(ix1,word)
     ...:             

In [710]: wordList
Out[710]: ['I', 'enjoy', 'smelling flowers', 'on', 'a', 'happy day']
[711]中的
s=''.join(posWords)
In[712]:s
出[712]:“美丽的爱快乐的日子快乐的闻花香”
在[672]中:n=[]
In[673]:对于单词表中的i:
…:如果我在s中:
…:n.附加(i)
...: 
In[713]:n
外出[713]:[‘享受’、‘闻’、‘花’、‘a’、‘快乐’、‘一天’]
In[740]:对于c,i在枚举(n)中:
…:如果c+1

如果有帮助,请告诉我。

以下是一种方法:

posWords = ['beautiful', 'love', 'happy day', 'enjoy','smelling flowers']
wordList = ['I', 'enjoy', 'smelling', 'flowers', 'on', 'a', 'happy', 'day'] 

# Create a sentence for the wordList.
joinedWordList = " ".join(wordList)

# Find all phrases in the posWords list.
phrases = [elem for elem in posWords if len(elem.split()) > 1]

# For every phrase, locate it in the sentence, 
# count the space characters which is the same number as the index of the first word of phrase in the word list,
# insert the phrase and delete the word that combine the phrase from the wordList.
for phrase in phrases:
    try:
        i = joinedWordList.index(phrase)
        spaces = len([letter for letter in joinedWordList[:i] if letter==' '])
        wordList.insert(spaces,phrase)
        del wordList[spaces+1:spaces+1 + len(phrase.split())]
    except ValueError:
        pass
print(wordList)
>>> m=["good bad", "enjoy", "play"]
>>> l=["good", "bad", "happy", "delight"]
>>>
>>> for e in m:
...     tmp = e.split(" ")
...     if(len(tmp) > 1):
...             l = [ent for ent in l if ent not in tmp]
...             l.append(" ".join(tmp))
...
>>>
>>> l
['happy', 'delight', 'good bad']
输出:

['I', 'enjoy', 'smelling flowers', 'on', 'a', 'happy day']

以下是另一种适用于任何短语长度的方法:

posWords = ['beautiful', 'love', 'happy day', 'enjoy','smelling flowers']
wordList = ['I', 'enjoy', 'smelling', 'flowers', 'on', 'a', 'happy', 'day'] 

for w in posWords:
    nrWords = len(w.split(' '))
    if nrWords > 1:
        word_array = w.split(' ')
        word_index_array = [wordList.index(w) for w in word_array]
        index_difference_array = [abs(b-a) for a in word_index_array[0:-1] for b in word_index_array[1:]]

        if sum(index_difference_array) == len(index_difference_array): #elements are consecutive in wordList
            for elem in word_array:
                wordList.remove(elem)                        
            wordList.insert(word_index_array[0], w)
输出将是:

['I', 'enjoy', 'smelling flowers', 'on', 'a', 'happy day']
['I', 'enjoy', 'smelling flowers on', 'a', 'happy day']
但如果我们输入以下内容:

posWords = ['beautiful', 'love', 'happy day', 'enjoy','smelling flowers on']
wordList = ['I', 'enjoy', 'smelling', 'flowers', 'on', 'a', 'happy', 'day']
输出将是:

['I', 'enjoy', 'smelling flowers', 'on', 'a', 'happy day']
['I', 'enjoy', 'smelling flowers on', 'a', 'happy day']
另一种方法是:

posWords = ['beautiful', 'love', 'happy day', 'enjoy','smelling flowers']
wordList = ['I', 'enjoy', 'smelling', 'flowers', 'on', 'a', 'happy', 'day'] 

# Create a sentence for the wordList.
joinedWordList = " ".join(wordList)

# Find all phrases in the posWords list.
phrases = [elem for elem in posWords if len(elem.split()) > 1]

# For every phrase, locate it in the sentence, 
# count the space characters which is the same number as the index of the first word of phrase in the word list,
# insert the phrase and delete the word that combine the phrase from the wordList.
for phrase in phrases:
    try:
        i = joinedWordList.index(phrase)
        spaces = len([letter for letter in joinedWordList[:i] if letter==' '])
        wordList.insert(spaces,phrase)
        del wordList[spaces+1:spaces+1 + len(phrase.split())]
    except ValueError:
        pass
print(wordList)
>>> m=["good bad", "enjoy", "play"]
>>> l=["good", "bad", "happy", "delight"]
>>>
>>> for e in m:
...     tmp = e.split(" ")
...     if(len(tmp) > 1):
...             l = [ent for ent in l if ent not in tmp]
...             l.append(" ".join(tmp))
...
>>>
>>> l
['happy', 'delight', 'good bad']

向我们展示您尝试过的内容。
'happy'
'day'
(例如)是否始终是
单词列表中的连续元素,或者它们是否会出现在任何位置?短语可能多于2个单词?是的,单词列表中的单词始终是连续顺序。这是问题的一部分,因为如果这些词出现在列表的其他地方,它们不会被视为一个积极的词,因为
快乐日
是一个积极的“词”@silkworm。是的,短语可以超过2个单词@Chris_Rands。你可以使用
表示c,i在枚举(n):
中删除
c=0
c+=1
。您还可以将
if-break-else
替换为简单的
if c+1
和不使用
else
break
。@Guimoute对此非常感谢。也编辑了我的答案。谢谢,这很有帮助!但是,当我传递更长的句子时(我正在荷兰报纸上做情绪分析),仍然有点麻烦。接下来的几天,我将尝试自己解决这个问题,以达到教学目的。当我找到解决方案或遇到问题时,我会及时通知你!谢谢,如果我试着用一个较长的句子,这个程序是有缺陷的,不能用正确的方式删除单词。但这对我帮助很大,接下来的几天我将尝试自己解决这个问题。顺便说一句,这是最好的解决方案,因为其他答案在两个以上的时间里都没有正确处理积极的词语words@mick_zon_24感谢您的反馈,您是否介意展示上述解决方案出现问题的具体情况?请这样做,以便我可以改进它。