【问题标题】:Alter the letter in list of strings更改字符串列表中的字母
【发布时间】:2017-10-05 00:44:24
【问题描述】:

一个例子:

eword_list =  ["a", "is", "bus", "on", "the"]
alter_the_list("A bus station is where a bus stops  A train station is where a train stops  On my desk I have a work station", word_list)
print("1.", word_list)

word_list =  ["a", 'up', "you", "it", "on", "the", 'is']
alter_the_list("It is up to YOU", word_list)
print("2.", word_list)

word_list =  ["easy", "come", "go"]
alter_the_list("Easy come easy go go go", word_list)
print("3.", word_list)

word_list =  ["a", "is", "i", "on"]
alter_the_list("", word_list)
print("4.", word_list)

word_list =  ["a", "is", "i", "on", "the"]
alter_the_list("May your coffee be strong and your Monday be short", word_list)
print("5.", word_list)

def alter_the_list(text, word_list):
    return[text for text in word_list if text in word_list]

我正在尝试从单词列表中删除作为文本字符串中单独单词的任何单词。在我检查单词列表的元素都是小写之前,应该将文本字符串转换为小写。文本字符串中没有标点符号,单词参数列表中的每个单词都是唯一的。我不知道如何解决它。

输出:

1. ['a', 'is', 'bus', 'on', 'the']
2. ['a', 'up', 'you', 'it', 'on', 'the', 'is']
3. ['easy', 'come', 'go']
4. ['a', 'is', 'i', 'on']
5. ['a', 'is', 'i', 'on', 'the']

预期:

 1. ['the']
 2. ['a', 'on', 'the']
 3. []
 4. ['a', 'is', 'i', 'on']
 5. ['a', 'is', 'i', 'on', 'the']

【问题讨论】:

  • list(set(word_list) - set(setence.lower().split()).

标签: python python-3.x


【解决方案1】:

我是这样做的:

def alter_the_list(text, word_list):
    for word in text.lower().split():
        if word in word_list:
            word_list.remove(word)

text.lower().split() 返回text 中所有以空格分隔的标记的列表。

关键是您需要更改 word_list。返回一个新的list 是不够的;您必须使用Python 3's list methods 就地修改列表。

【讨论】:

    【解决方案2】:

    如果结果列表的顺序无关紧要,您可以使用集合:

    def alter_the_list(text, word_list):
        word_list[:] = set(word_list).difference(text.lower().split())
    

    由于使用word_list[:] = ... 分配给列表切片,此函数将更新word_list

    【讨论】:

    • 这一定是我见过的最快的编辑和否决。
    • 好吧,我只负责其中的 一个 :-) 我实际上认为这是一个有用的答案。 +1
    【解决方案3】:

    1 您的主要问题是您从函数返回一个值,然后忽略它。您必须以某种方式保存它才能打印出来,例如:

    word_list =  ["easy", "come", "go"]
    word_out = alter_the_list("Easy come easy go go go", word_list)
    print("3.", word_out)
    

    你打印的是原词表,不是函数结果。

    2 您忽略了函数的 text 参数。您在列表理解中重用变量名称作为循环索引。获取不同的变量名,如

    return[word for word in word_list if word in word_list]
    

    3 您仍然必须将 text 包含在您构建的列表的逻辑中。请记住,您正在寻找给定文本中非的词。

    最重要的是,学习基本的调试。 请参阅这个可爱的 debug 博客寻求帮助。

    如果不出意外,请学习使用简单的 print 语句来显示变量的值,并跟踪程序执行。

    这是否会让您朝着解决方案迈进?

    【讨论】:

      【解决方案4】:

      我更喜欢@Simon 的回答,但如果你想在两个列表推导中做到这一点:

      def alter_the_list(text, word_list):
          # Pull out all words found in the word list
          c = [w for w in word_list for t in text.split() if t == w]
          # Find the difference of the two lists
          return [w for w in word_list if w not in c]
      

      【讨论】:

      • 这实际上可以在一个列表理解中完成:[word for word in word_list if word not in setence.lower().split()] 并且它仍然具有相当的可读性。
      猜你喜欢
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      相关资源
      最近更新 更多