我想根据(n-1)-元组列表和一个列表创建一个n-元组列表。
下面是一个最小化的例子,n=3,列表长度为5。
import itertools
# create a list with 5 elements
t1_list = [x for x in range(1,6)]
print(t1_list) # [1, 2, 3, 4, 5]
# create 2-tuples out of t1_list
t2 = itertools.combinations(t1_list,2)
t2_list = [x for x in t2]
print(t2_list) # [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
# remove some tuples based on some criterion
del t2_list[1]
del t2_list[4]
print(t2_list) # [(1, 2), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (4, 5)]
# now create a list of 3-tuples based on t2_list and t1_list
# i.e. without combinations containing (1,3) and (2,4) because they have been removed
# ???
# Result should be:
# [(1, 2, 5), (1, 4, 5), (2, 3, 5), (3, 4, 5)]
我想,从列表中创建所有可能的n-元组是不是一个好主意?t1_list
然后删除包含被删除的(n-1)-元组的元组,因为在以后的阶段,我想对100个元素列表中最多10个元组进行删除。
是否有一种聪明的方法可以通过使用 itertools
?
编辑。 澄清如何得到想要的结果。
(1, 2, 4)
被排除在外,因为 (2, 4)
不在 t2_list
和 (1, 2, 3)
被排除在外,因为 (1,3)
不在 t2_list
.
一般规则是:一个n-tuple只有当它的所有(n-1)-tuples都存在于 t(n-1)_list
. 或者反过来说:如果一个n-tuple的一个或多个(n-1)-tuple缺少在 t(n-1)_list
.
这就是我在这段时间里所取得的进展。下面的例子显示了一个长度为6的列表(与原来的问题不同),结果似乎是正确的。然而,我不知道这是否是好的和有效的代码,特别是当 t1_list
将会有100个元素,而且它可以到8-或10-tuple。我需要进一步测试。非常欢迎大家提出改进建议。
编码。
### create n-tuples list based on (n-1)-tuples list and a list
import itertools
# create a list with nmax elements
nmax = 6
t1_list = [x for x in range(1,nmax+1)]
print("T1_list:", t1_list)
# create 2-tuples out of t1_list
t2 = itertools.combinations(t1_list,2)
t2_list = [x for x in t2]
print("T2_list:", t2_list)
# remove some tuples based on a calculation result
del t2_list[1]
del t2_list[4]
print("T2_list after removal of some elements:", t2_list)
def create_ntuple_list(tn1_list, t1_list): # create n-tuples out of (n-1)_list and t1_list
tn_list = []
for tn1 in tn1_list: # i.e. t(n-1)_list
n = len(tn1) + 1 # determine n
tn1max = max(tn1)
for t1 in t1_list:
if t1 > tn1max:
tn_tmp = tuple(list(tn1)+[t1])
tn1_iter = itertools.combinations(tn_tmp,n-1)
for tn1_tmp in tn1_iter:
if tn1_tmp not in tn1_list:
break
else:
tn_list.append(tn_tmp)
return tn_list
# create 3-tuples
t3_list = create_ntuple_list(t2_list, t1_list)
print("T3_list:", t3_list)
# remove some tuples based on a calculation result
del t3_list[2]
print("T3_list after removal of some elements", t3_list)
# create 4-tuples
t4_list = create_ntuple_list(t3_list, t1_list)
print("T4_list:", t4_list)
### end of code
结果:
T1_list: [1, 2, 3, 4, 5, 6]
T2_list: [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]
T2_list after removal of some elements: [(1, 2), (1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]
T3_list: [(1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)]
T3_list after removal of some elements [(1, 2, 4), (1, 2, 5), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)]
T4_list: [(1, 2, 4, 5), (1, 4, 5, 6), (2, 4, 5, 6), (3, 4, 5, 6)]