2014-05-05 31 views
5

我需要幫助從列表中順序返回唯一字(不區分大小寫)。Python從列表中返回唯一字(不區分大小寫)

例如:

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」(我假設範圍功能將是有益的)

,並使其返回首先來自該功能將在原始列表中字

會有怎樣。我這樣做使用for循環

回答

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

你能做到這一點的:

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

這是完成這個任務的一個瘋狂低效的方式。 – roippi

1

您可以使用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'] 
+0

「我將如何使用for循環執行此操作?」 –

+2

-1在LC中有副作用不是首選。 – thefourtheye

+0

@zmo LC中的副作用並不高雅。 – thefourtheye

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

您可以使用一組,以確保其唯一性。當你嘗試添加一個重複項目到一個集合時,它會簡單地丟棄它,如果它已經在那裏。

您還應該使用in-built lower()函數來管理不區分大小寫。

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

如果這是一門功課的任務,並使用一套是關閉的限制,那麼你可以很容易地適應它通過只使用名單,只是循環並添加條件:

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

OK,去除我之前的回答,因爲我誤解了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=", ") 
+1

「將返回:[」我們「,」是「,」一個「,」這個「,」世界「,」宇宙「 ]「 –

+1

'而且他並沒有說他想要保持令牌的順序:''我需要幫助從列表中**返回唯一的單詞(不區分大小寫)**」 – roippi

+0

好吧,誤讀: - )我的壞,但我有另一種解決方案,所以我編輯 – zmo

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

啊。我打算髮布相同的回覆! –

相關問題