简体   繁体   English

如何基于python嵌套列表中的公共元素计算子列表的数量?

[英]How to count the number of sublists based on common elements from a nested list in python?

I have a list of lists like this: [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3], [3, 4]] . 我有一个这样的列表列表: [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3], [3, 4]] How can I count the lists which are sublists of more than two lists? 如何计算属于两个以上列表的子列表的列表? For example, here [2, 3] and [3, 4] would be the lists that are sublists of first 3 lists. 例如,这里[2, 3] and [3, 4]将是前3个列表的子列表。 I want to get rid of them. 我想摆脱它们。

This comprehension should do it: 这种理解应该做到:

data = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3], [3, 4]]
solution = [i for i in data if sum([1 for j in data if set(i).issubset(set(j))]) < 3]
set_list = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3], [3, 4]]
check_list = [[2, 3], [3, 4]]
sublist_to_list = {}

for set in set_list:
    for i, sublist in enumerate(check_list):
        count = 0
        for element in sublist:
            if element in set:
                count += 1

        if count == len(sublist):
            if i not in sublist_to_list:
                sublist_to_list[i] = [set]
            else:
                sublist_to_list[i].append(set)

print(sublist_to_list)

Output: {0: [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3]], 1: [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [3, 4]]} 输出: {0: [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3]], 1: [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [3, 4]]}

  • which means [2, 3] is subset of [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3]] 这表示[2,3]是[[1、2、3、4、5、6、7、8、9],[1、2、3、4、5],[2、3、4, 5,6,7],[2,3]]
  • and [3, 4] is subset of [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [3, 4]] 并且[3,4]是[[1,2,3,4,5,6,7,8,9],[1、2、3、4、5],[2、3、4、5 ,6,7],[3,4]]

You can first make a function that gets sub lists of a list: 您可以首先创建一个获取列表子列表的函数:

def sublists(lst):
    length = len(lst)

    for size in range(1, length + 1):
        for start in range(length - size + 1):
            yield lst[start:start+size]

Which works as follows: 其工作原理如下:

>>> list(sublists([1, 2, 3, 4, 5]))
[[1], [2], [3], [4], [5], [1, 2], [2, 3], [3, 4], [4, 5], [1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 2, 3, 4], [2, 3, 4, 5], [1, 2, 3, 4, 5]]

Then you can use this to collect all the sublists list indices into a collections.defaultdict : 然后,您可以使用它来将所有子列表列表索引collections.defaultdictcollections.defaultdict

from collections import defaultdict

lsts = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6, 7], [2, 3], [3, 4]]

d = defaultdict(list)
for i, lst in enumerate(lsts):
    subs = sublists(lst)
    while True:
        try:
            curr = tuple(next(subs))
            d[curr].append(i)
        except StopIteration:
            break

Which will have tuple keys for the sublists, and the list indices as the values. 它将具有用于子列表的元组键,以及列表索引作为值。

Then to determine sub lists that occur more than twice in all the lists, you can check if the set of all the indices has a length of more than two: 然后,要确定在所有列表中出现两次以上的子列表,可以检查所有索引的集合的长度是否大于两个:

print([list(k) for k, v in d.items() if len(set(v)) > 2])

Which will give the following sublists: 这将给出以下子列表:

[[2], [3], [4], [5], [2, 3], [3, 4], [4, 5], [2, 3, 4], [3, 4, 5], [2, 3, 4, 5]]

暂无
暂无

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

相关问题 根据元素的比较从嵌套列表中删除项目(子列表) - Remove items (sublists) from nested list based on comparison of their elements 如何根据python3中的元素序列在列表中创建子列表? - How to create sublists within a list based on a sequence of elements in python3? 如何根据开始和结束元素从列表创建子列表? - How to create sublists from list based on start and end elements? Python:如何从列表中的句子元素制作多个字符串子列表? - Python: How to make multiple string sublists from sentence elements in list? 如何基于索引添加子列表的元素-Python - How to add the elements of sublists based on index - Python Python从嵌套列表中提取所有子列表 - Python extract all sublists from a nested list Python - 如何根据字符串的一部分从字符串列表中创建子列表? - Python - How to create sublists from list of strings based on part of the string? 如何基于python中的字符串从列表中创建子列表? - How can I make sublists from a list based on strings in python? 如何将python中的嵌套列表解压缩为子列表? - How to unpack a nested list in python to sublists? 如何首先基于初始列表的单个元素将列表拆分为子列表,然后仅在 python 中将列表的连续部分拆分为子列表? - How to split a list into sublists based on single elements of the initial list first and then contiguous portions of the list simply in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM