如何将值设置为与上一项相同?

时间:2018-08-06 14:37:48

标签: python python-3.x

我有一个文本,将我分成句子列表,我想找到每个句子的主题。例如,如果文本为“狗很棒。他们太棒了”,必须将它们分为两个句子“狗很棒”。和“它们真棒”。然后,我使用一个for循环来查找每个句子的主题,无论是“猫”还是“狗”。

sentence_list=['Dogs are great', 'They are so awesome']
for sentence in sentence_list:
    if 'Dog' in sentence:
        subject= 'Dog'
    elif 'Cat' in sentence:
        subject='Cat'

因为“它们”被替换为其中一个,所以我想将该句子的主题设置为与最后一个句子相同。因此,在此示例中,两个句子的主题均为“狗”。

3 个答案:

答案 0 :(得分:1)

您已经有了最后一个值。如果if子句和elif子句都不为真,则此迭代未设置subject。这意味着它将仍然保持与上次迭代时相同的值。

sentence_list=['Dogs are great', 'They are so awesome']
for sentence in sentence_list:
    if 'Dog' in sentence:
        subject= 'Dog'
    elif 'Cat' in sentence:
        subject='Cat'
    print(subject)

将导致:

Dog
Dog

答案 1 :(得分:0)

我建议您使用startswith()字符串方法来检查句子是否以“他们”或“ It”开头,然后用前面句子的主题进行替换。这很简单,可能在复杂的句子上会失败,但是可以解决您的问题:

sentence_list=['Dogs are great', 'They are so awesome','Cats are nice', 'They can be dangerous']

for i,sentence in enumerate(sentence_list):
    if sentence.startswith('Dogs'):
        subject= 'Dogs'
    elif sentence.startswith('Cats'):
        subject='Cats'
    if sentence.startswith('They'):
        sentence_list[i] = sentence.replace('They', subject)

print(sentence_list)
# ['Dogs are great', 'Dogs are so awesome', 'Cats are nice', 'Cats can be dangerous']

答案 2 :(得分:0)

此解决方案在处理复数,代词选择以及查找可能不是句子第一个单词的代词方面更具灵活性。

这可能会超出您的范围,因为您将陷入动词时态问题,但我认为这可能对其他人有所帮助。

sentence_list = ['Dogs are great', 'They are so awesome', 'Cats are nice', 'They can be dangerous',
                 'On the lonely roads, they can be found.', 'He is fluffy.']
new_list = []
pronowns = ['they', 'them', 'she', 'her', 'he', 'him', 'us', 'we', 'it']
plurals = ['they', 'them', 'us', 'we']
last_subject = 'Dog'

for i, sentence in enumerate(sentence_list):

    # update last subject
    if 'Dog' in sentence:
        last_subject = 'Dog'
    elif 'Cat' in sentence:
        last_subject = 'Cat'

    if 'dog' not in sentence.lower() and 'cat' not in sentence.lower():
        # find pronoun
        for pn in pronowns:
            if pn in sentence.lower():
                # if it a plural usage add s
                if pn in plurals:
                    sentence_list[i] = sentence.lower().replace(pn, last_subject + 's')
                else:
                    sentence_list[i] = sentence.lower().replace(pn, last_subject)
                break


print(sentence_list)

输出:

['Dogs are great', 
'Dogs are so awesome', 
'Cats are nice', 
'Cats can be dangerous', 
'on the lonely roads, Cats can be found.', 
'Cat is fluffy.']