简体   繁体   English

将字符串列表转换为唯一的小写字母,保留顺序(python 2.7)

[英]convert list of strings to unique lowercase, preserving order (python 2.7)

I'd like to convert a list of strings to lowercase and remove duplicates while preserving the order. 我想将字符串列表转换为小写并在保留顺序的同时删除重复项。 A lot of the single-line Python magic I've found on StackOverflow converts a list of strings to lowercase, but it seems the order is lost. 我在StackOverflow上发现的许多单行Python魔术将字符串列表转换为小写字母,但似乎顺序丢失了。

I've written the code below which actually works, and I'm happy to stick it with. 我已经编写了下面的代码,该代码可以正常工作,并且很高兴坚持使用。 But I was wondering if there is a way of doing it that is a lot more pythonic and less code (and potentially less buggy if I were to write something similar in the future. This one took me quite a while to write). 但是我想知道是否有一种方法可以实现更多的Python语言和更少的代码(如果将来我要写类似的东西,那么可能会减少错误的发生。这花了我相当长的时间来写)。

def word_list_to_lower(words):
    """ takes a word list with a special order (e.g. frequency)
    returns a new word list all in lower case with no uniques but preserving order"""

    print("word_list_to_lower")    
    # save orders in a dict
    orders = dict()
    for i in range(len(words)):
        wl = words[i].lower()

        # save index of first occurence of the word (prioritizing top value)        
        if wl not in orders:
            orders[wl] = i

    # contains unique lower case words, but in wrong order
    words_unique = list(set(map(str.lower, words)))

    # reconstruct sparse list in correct order
    words_lower = [''] * len(words)
    for w in words_unique:
        i = orders[w]
        words_lower[i] = w

    # remove blank entries
    words_lower = [s for s in words_lower if s!='']

    return words_lower

Slightly modifying the answer from How do you remove duplicates from a list in whilst preserving order? 略微修改“ 如何在保留顺序的同时从列表中删除重复项”中的答案

def f7(seq):
    seen = set()
    seen_add = seen.add
    seq = (x.lower() for x in seq)
    return [x for x in seq if not (x in seen or seen_add(x))]

You can also do: 您也可以:

pip install orderedset

and then: 接着:

from orderedset import OrderedSet
initial_list = ['ONE','one','TWO','two','THREE','three']
unique_list =  [x.lower() for x in list(OrderedSet(initial_list))]

print unique_list

Just do something like: 只需执行以下操作:

initial_list = ['ONE','one','TWO','two']
uninique_list =  [x.lower() for x in list(set(initial_list))]

print unique_list
initial_list = ['ONE','one','TWO','two']
new_list = []
[new_list.append(s.lower()) for s in initial_list if s.lower() not in new_list]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM