Python 从嵌套列表中删除类似元素
我正在使用以下代码创建嵌套列表:Python 从嵌套列表中删除类似元素,python,list,itertools,Python,List,Itertools,我正在使用以下代码创建嵌套列表: from itertools import combinations def get_nested_list(arr, n, k): iterator = 0 nest = [] for i in range(1, n): for combo in combinations(arr, i): odd_num = 0 for item in combo:
from itertools import combinations
def get_nested_list(arr, n, k):
iterator = 0
nest = []
for i in range(1, n):
for combo in combinations(arr, i):
odd_num = 0
for item in combo:
if item % 2 != 0:
odd_num += 1
if odd_num <= k:
iterator += 1
nest.append(combo)
nest = [list(i) for i in nest]
return nest
a = [1, 2, 3, 4]
b = len(a)
c = 1
print(get_nested_list(a, b, c))
但是,我得到了具有相同x值的[1,2]
和[1,4]
元素,以及具有相同y值的[2,4]
和[3,4]
元素。如何创建具有完全不同子列表的列表?这就是我试图实现的目标:
[[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [2, 3, 4]]
与其删除相似的,首先不要麻烦生成它们。实际上,您不需要组合,而是需要重叠的块(除了那些包含多个奇数项的块,您的代码已经说明了这一点,我在这里进行了调整)
def重叠块(大小、顺序):
“”“产生重叠的序列块。”“”
对于范围内的i(长度(序列)-大小+1):
产量序列[i:i+尺寸]
def powerchunk(序列):
“”“产生所有重叠的序列块,类似于powerset。”“”
对于范围(1,长度(序列)+1)内的尺寸:
重叠块的产量(大小、顺序)
a=[1,2,3,4]
最大赔率=1
结果=[
powerchunk中的块对块(a)
如果总和(块中项目的项目%2!=0)[[1]、[2]、[3]、[4]、[1,2]、[2,3]、[3,4]、[2,3,4]]
相关:所选答案显然更简单,但我一直在做这件事,所以我还是发布了我的答案。它使用
defaultdict
和一些逻辑来检查该索引中是否存在长度相同的现有条目
from itertools import combinations
from collections import defaultdict
def get_nested_list(arr, n, k):
iterator = 0
nest = []
d = defaultdict(set)
for i in range(1, n):
for combo in combinations(arr, i):
odd_num = 0
for item in combo:
if item % 2 != 0:
odd_num += 1
if odd_num <= k:
iterator += 1
skip = False
len_c = len(combo)
for j, n in enumerate(combo):
if n in d.get((len_c, j), set()):
skip = True
break
d[(len_c, j)].add(n)
if skip: continue
nest.append(combo)
nest = [list(i) for i in nest]
return nest
a = [1, 2, 3, 4]
b = len(a)
c = 1
res = get_nested_list(a, b, c)
print(res)
assert res == [[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 4]]
来自itertools导入组合的
从集合导入defaultdict
def get_嵌套_列表(arr、n、k):
迭代器=0
嵌套=[]
d=默认DICT(设置)
对于范围(1,n)内的i:
对于组合中的组合(arr,i):
奇数=0
对于组合中的项目:
如果项目%2!=0:
奇数_num+=1
如果奇数,sequence
和size
相对于我的代码代表什么?sequence
在代码中是arr
,而size
是i
(表示范围(1,n)
)这些是嵌套的定义吗?你是如何在get\u nested\u list
之外访问i的?因为我没有使用你的代码作为源代码,所以结构完全不同。重叠块
是我已经拥有的一个函数,powerchunk
基于一个.Got.Thanx m8。我会接受你的答案并重新考虑dy投了赞成票。如果你认为这是一个问得很好的问题,你也可以投我一票吗?为什么[1,2,3]
没有出现在预期的结果中?好的,为什么[1,2,4]
没有出现在最后的列表中?所以这也是一个有效的答案?[1]、[2]、[3]、[4]、[1,2]、[2,3]、[3,4]、[1,2,4]]
可能是。我没有研究t的算法。因为如果你真的运行你的代码,第一个添加的是[1,2,4],因此不应该添加[2,3,4]是理所当然的。在另一个例子上发布我的答案??我对你的意思感到困惑?它返回:[[1]、[2]、[3]、[4]、[1,2]、[2,3]、[3,4]、[1,2,4]]
并且该值被分配给res
哦,对不起,我运行的是另一个文件。我的不好。实际上,你的代码引发了一些问题。对于a=[3,2,3,4]
和k=1
,输出应该是7,但它给我6。对于a=[2,5,6,9,11,9,2,11,12]
和k=1
,输出应为18,但其给定值为17。
def overlapping_chunks(size, sequence):
"""Yield overlapping chunks of sequence."""
for i in range(len(sequence)-size+1):
yield sequence[i:i+size]
def powerchunk(sequence):
"""Yield all overlapping chunks of sequence, similar to a powerset."""
for size in range(1, len(sequence)+1):
yield from overlapping_chunks(size, sequence)
a = [1, 2, 3, 4]
max_odds = 1
result = [
chunk for chunk in powerchunk(a)
if sum(item % 2 != 0 for item in chunk) <= max_odds
]
print(result) # -> [[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [2, 3, 4]]
from itertools import combinations
from collections import defaultdict
def get_nested_list(arr, n, k):
iterator = 0
nest = []
d = defaultdict(set)
for i in range(1, n):
for combo in combinations(arr, i):
odd_num = 0
for item in combo:
if item % 2 != 0:
odd_num += 1
if odd_num <= k:
iterator += 1
skip = False
len_c = len(combo)
for j, n in enumerate(combo):
if n in d.get((len_c, j), set()):
skip = True
break
d[(len_c, j)].add(n)
if skip: continue
nest.append(combo)
nest = [list(i) for i in nest]
return nest
a = [1, 2, 3, 4]
b = len(a)
c = 1
res = get_nested_list(a, b, c)
print(res)
assert res == [[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 4]]