python中的集合不区分大小写 [英] case insensitive for sets in python
问题描述
我有一个由多个列表生成的列表.该组合列表包含最终用户生成的名称.因此,包含相似的名称,但具有不同的大写/小写字符. 我想过滤掉包含相同字符的名称,而只将第一个保留在原始列表中.
I have a list that is generated from multiple lists. This combined list contains names that are generated by end users. Therefore contain similar names, but with different upper/lower case characters. I want to filter out the names that contain same characters and just keep the first found in the original list.
例如,我有以下列表:
L0 = ['A_B Cdef', 'A_B Cdef', 'A_B Cdef', 'A_B CdEF', 'A_B CDEF','a_B CdEF', 'A_b CDEF', 'GG_ooo', 'a1-23456']
如果我跑步:
L1 = list(set(L0))
我得到:
['a1-23456', 'A_B Cdef', 'A_B CdEF', 'A_B CDEF', 'a_B CdEF', 'A_b CDEF', 'GG_ooo']
我只想保留具有相同字符的名字中的第一个.
I would like to keep just the first of the names that have same characters.
所以我的结果是:
['a1-23456', 'A_B Cdef', 'GG_ooo']
如果我使用.lower()
,则.upper()
会得到列表,但是名称是小写/大写.
If I use .lower()
, .upper()
I get the list, but the names are lower/upper cased.
我只想消除重复项",而不考虑区分大小写的方法.
I just want to eliminate "duplicates" without considering case sensitive approach.
非常感谢您的帮助.
谢谢!
推荐答案
请改用散列,我认为您无法轻松地使用集合来完成该操作.
Use hash instead, I don't think you can accomplish that easily with sets.
L0 = {value.lower(): value for value in L0[::-1]}.values()
这篇关于python中的集合不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!