【问题标题】:How do I set a value to be the same as in the last item?如何将值设置为与最后一项相同?
【发布时间】:2018-08-06 14:37:48
【问题描述】:

我有一个要拆分成句子列表的文本,我想找到每个句子的主题。例如,如果文本是“狗很棒。他们太棒了”,他们必须分成“狗很棒”两句话。和“他们太棒了”。然后我使用for循环来查找每个句子的主题是什么,是'cats'还是'dogs'。

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'

因为“他们”被用作其中一个的替代品,所以我想将该句子的主题设置为与最后一个句子相同。所以在这个例子中,两个句子的主语都是“狗”。

【问题讨论】:

标签: python python-3.x


【解决方案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

【讨论】:

    【解决方案2】:

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

    这可能超出了你的范围,因为那时你会遇到动词时态问题,但我认为它可能对其他人有帮助。

    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.']
    

    【讨论】:

      【解决方案3】:

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

      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']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-26
        • 2014-01-14
        • 1970-01-01
        相关资源
        最近更新 更多