简体   繁体   English

列表和元素比较python

[英]list and elements comparison python

list1 = ['A','B']
list2 = ['a','c']
list3 = ['x','y','z']
list4 = [['A','b','c'],['a','x'],['Y','Z'],['d','g']]

I want to check if all elements of list (list1, list2, list3) is contained in any of list in another bigger list (list4). 我想检查列表的所有元素(列表1,列表2,列表3)是否包含在另一个更大列表(列表4)中的任何列表中。

I want the comparison to be case insensitive. 我希望比较不区分大小写。

To be sure, here list1 and list2 is in list4 but not list3. 可以肯定的是,这里的list1和list2在list4中,但不在list3中。 How can I do it? 我该怎么做?

On the other note, How would I know if a list is collection of list. 另一方面,如何知道列表是否为列表的集合。

In other words, how can I distinguish if list is a collection of list of just list of elements, if I am not the one who is defining the lists. 换句话说,如果我不是定义列表的人,那么如何区分list是仅元素列表的列表的集合。

First item - you want to do case-insensitive matching. 第一项-您要进行不区分大小写的匹配。 The best way to do that is to convert everything to one case (upper or lower). 最好的方法是将所有内容都转换为一个大小写(大写或小写)。 So for each list, run 因此,对于每个列表,运行

list1 = map(lambda x: x.lower(), list1)

That will convert your lists to lowercase. 这会将您的列表转换为小写。 Let's assume you've done that. 假设您已经完成了。

Second, for a comparison of two "simple" lists (not-nested), you can simply say 其次,对于两个“简单”列表(非嵌套)的比较,您可以简单地说

if set(list1) < set(list2):

to compare if list1 is a subset of list2. 比较list1是否为list2的子集。 In your example, it would be false. 在您的示例中,这将是错误的。

Finally, if you want to check if a list is nested: 最后,如果要检查列表是否嵌套:

if ( type(list4[0]) == list) :

which in this case, would be true. 在这种情况下,这将是正确的。 Then, just iterate over the elements of list4 and do the set comparison above. 然后,只需遍历list4的元素并进行上面的集合比较。

You can use lower() to make all elements of all lists to lowercase to achieve case-insensitivity. 您可以使用lower()使所有列表的所有元素都变为小写,以实现不区分大小写。

def make_case_insensitive(lst):
    return [i.lower() for i in lst]

For example, 例如,

list1=make_case_insensitive(list1)

As, biggerlist is slightly different (contains list as element), you have to change the function slightly. 由于biglist稍有不同(包含list作为元素),因此您必须稍微更改功能。

def make_bigger_list_caseinsensitive(bigger_list):
    return [[i.lower() for i in element] for element in bigger_list]

list4=make_bigger_list_caseinsensitive(list4)

Check if any element of the biggerlist is the superset of smaller set. 检查更大列表中的任何元素是否是较小集合的超集。 Print Is in bigger list if condition satisfied, print not in biggger list otherwise. 如果满足条件,则在较大列表中打印否则不在 较大列表中打印。 Make set from the list first. 首先从list set

print "Is in bigger list" if any(set(element).issuperset(set(list1)) for element in list4) else "not in biggger list"

To write it with slightly more readability, do: 要使其更具可读性,请执行以下操作:

if any(set(element).issuperset(set(list1)) for element in list4):
    print "Is in bigger list"
else:
    print "not in biggger list"

Finally,to check if nested list exists in biggerlist: 最后,检查在更大列表中是否存在嵌套列表:

print any(type(element)==list for element in list4)

Using set is a good way. 使用set是一个好方法。

list1 = ['A','B']
list2 = ['a','c']
list3 = ['x','y','z']
list4 = [['A','b','c'],['a','x'],['Y','Z'],['d','g']]

set1 = set(map(lambda s: s.lower(), list1))
set2 = set(map(lambda s: s.lower(), list2))
set3 = set(map(lambda s: s.lower(), list3))
set4 = map(lambda l: set(map(lambda s: s.lower(), l)), list4)

print(set1) # set(['a', 'b'])
print(set2) # set(['a', 'c'])
print(set3) # set(['y', 'x', 'z'])
print(set4) # [set(['a', 'c', 'b']), set(['a', 'x']), set(['y', 'z']), set(['d', 'g'])]

lor = lambda x, y: x or y

reduce(lor, map(lambda s: set1.issubset(s), set4)) # True
reduce(lor, map(lambda s: set2.issubset(s), set4)) # True
reduce(lor, map(lambda s: set3.issubset(s), set4)) # False
  • To do a case-insensitive string comparison, covert both strings to lowercase or uppercase. 要进行不区分大小写的字符串比较,请将两个字符串都转换为小写或大写。
  • To test all elements in list1 are contained in list4 , use set.issubset . 为了测试中的所有元素list1中包含的list4 ,使用set.issubset

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

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