itertools:如何从(n1)元组列表和一个列表中生成N元组列表?

2024-04-26 11:07:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想基于(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)]

我想从t1_list中创建所有可能的n元组,然后删除包含已删除(n-1)元组的元组不是一个好主意,因为在稍后的阶段,我希望使用100个元素列表中的10个元组来执行此操作

有没有一种聪明的方法可以用itertools实现这一点

编辑:如何获得所需结果的说明:

(1, 2, 4)被排除是因为(2, 4)不在t2_list中,而(1, 2, 3)被排除是因为(1,3)不在t2_list

一般规则是:只有当n元组的所有(n-1)元组都存在于t(n-1)_list中时,才会包含n元组。或者反过来说:如果n元组的一个或多个(n-1)元组在t(n-1)_list中丢失,则n元组将被排除


Tags: ofin列表forcreatesomelistbased
1条回答
网友
1楼 · 发布于 2024-04-26 11:07:00

这就是我在这段时间里取得的成就。下面的示例显示了长度为6的列表(与原始问题不同)。 结果似乎是正确的。然而,我不知道这是否是一个好的、高效的代码,特别是当t1_list将有100个元素,并且它将增加到8或10个元组时。我需要进一步测试。欢迎提出改进建议

代码:

### 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)]

相关问题 更多 >