Python从列表中返回唯一的单词(不区分大小写)

时间:2014-05-05 13:01:06

标签: python for-loop

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

例如:

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" (我假设范围功能很有用)

并使其返回原始列表中首先出现的单词,该单词将起作用。

如何使用 for循环

执行此操作

7 个答案:

答案 0 :(得分:7)

您可以借助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']

答案 1 :(得分: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']

答案 2 :(得分:1)

你可以这样做:

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

您可以使用一组来确保唯一性。当您尝试将重复项目添加到某个集合时,如果它已经存在,则会将其丢弃。

您还应该使用内置的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

答案 5 :(得分:1)

您可以像这样使用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']

答案 6 :(得分:0)

好的,删除了我以前的答案,因为我误读了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=", ")