【问题标题】:Python returning unique words from a list (case insensitive)Python 从列表中返回唯一单词(不区分大小写)
【发布时间】:2014-05-05 13:01:05
【问题描述】:

我需要帮助才能按顺序从列表中返回唯一字词(不区分大小写)。

例如:

def case_insensitive_unique_list(["We", "are", "one", "we", "are", "the", "world", "we", "are", "THE", "UNIVERSE"])

将返回: [“我们”、“是”、“一个”、“这个”、“世界”、“宇宙”]

到目前为止,这是我所拥有的:

def case_insensitive_unique_list(list_string):

uppercase = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
lowercase = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

temp_unique_list = []

for i in list_string:
    if i not in list_string:
        temp_unique_list.append(i)

我无法比较 temp_unique_list 中的每个单词,无论该单词是否重复。例如:“to”和“To”(我假设 range 函数会很有用)

并使其返回该函数将接收的原始列表中第一个出现的单词。

我将如何使用 for 循环来做到这一点?

【问题讨论】:

    标签: python for-loop


    【解决方案1】:

    您可以借助 for 循环和 set 数据结构来做到这一点,就像这样

    def case_insensitive_unique_list(data):
        seen, result = set(), []
        for item in data:
            if item.lower() not in seen:
                seen.add(item.lower())
                result.append(item)
        return result
    

    输出

    ['We', 'are', 'one', 'the', 'world', 'UNIVERSE']
    

    【讨论】:

      【解决方案2】:

      您可以使用set() 和列表理解:

      >>> seen = set()
      >>> lst = ["We", "are", "one", "we", "are", "the", "world", "we", "are", "THE", "UNIVERSE"]
      >>> [x for x in lst if x.lower() not in seen and not seen.add(x.lower())]
      ['We', 'are', 'one', 'the', 'world', 'UNIVERSE']
      

      【讨论】:

      • “我将如何使用 for 循环来做到这一点?”
      • @zmo 在 LC 中有副作用并不优雅。
      • @zmo 即使 for 关键字用于列表理解,我很确定这不是 OP 想要的
      • 我不同意我的同事的观点,我认为这是一个很好的维持秩序的成语。我经常使用它。 (列表组合是否在我不知道或不关心的 OP 想要的范围内)
      【解决方案3】:

      你可以这样做:

      l = ["We", "are", "one", "we", "are", "the", "world", "we", "are", "THE", "UNIVERSE"]
      
      a = []
      
      for i in l:
          if i.lower() not in [j.lower() for j in a]:
              a.append(i)
      
      >>> print a
      ['We', 'are', 'one', 'the', 'world', 'UNIVERSE']
      

      【讨论】:

      • 这是一种非常低效的方法。
      【解决方案4】:
      l=["We", "are", "one", "we", "are", "the", "world", "we", "are", "THE", "UNIVERSE"]
      so=[]
      for w in l:
          if w.lower() not in so:
              so.append(w.lower())
      
      In [14]: so
      Out[14]: ['we', 'are', 'one', 'the', 'world', 'universe']
      

      【讨论】:

        【解决方案5】:

        您可以使用集合来确保唯一性。当您尝试将重复项添加到集合中时,如果它已经存在,它将简单地丢弃它。

        您还应该使用内置的 lower() 函数来管理不区分大小写。

        uniques = set()
        for word in words:
            set.add(word.lower()) #lower it first and then add it
        

        如果这是用于家庭作业并且使用 set 是禁止的,那么您可以轻松地将其调整为仅使用列表,只需循环并添加条件:

        uniques = list()
        if word.lower() not in uniques:
            #etc
        

        【讨论】:

          【解决方案6】:

          你可以像这样使用collections.OrderedDict

          from collections import OrderedDict
          def case_insensitive_unique_list(data):
              d = OrderedDict()
              for word in data:
                  d.setdefault(word.lower(), word)
              return d.values()
          

          输出:

          ['We', 'are', 'one', 'the', 'world', 'UNIVERSE']
          

          【讨论】:

          • 啊。我打算发布同样的回复!
          【解决方案7】:

          好的,删除了我之前的答案,因为我误读了 OP 的帖子。我所有的歉意。

          作为一个借口,为了它的乐趣和以不同的方式做它,这里有另一种解决方案,虽然它既不是最有效的,也不是最好的:

          >>> from functools import reduce
          >>> for it in reduce(lambda l,it: l if it in set({i.lower() for i in l}) else l+[it], lst, []):
          ...     print(it, end=", ")
          

          【讨论】:

          • "将返回:["We", "are", "one", "the", "world", "UNIVERSE"]"
          • And also as he does not say he wants to keep the token in order: "我需要帮助以按顺序返回列表中的唯一单词(不区分大小写)。"
          • 好的,误读 :-) 我的错,虽然我有另一个解决方案,所以我正在编辑
          猜你喜欢
          • 2021-12-01
          • 2021-11-04
          • 2015-08-30
          • 2013-03-22
          • 1970-01-01
          • 2012-01-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多