python编程实现:分别从三个(多个)列表中抽取一个元素,组成新的列表,类似组合问题的求解


# 例:从列表list1, list2, list3 中每次各抽取一个元素,再将抽取的元素来组成新的列表
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
list3 = [11, 12, 13, 14, 15]
index_set = []
temp_set = []
result_set = []
for i in list1:
    for j in list2:
        for k in list3:
            index_set.append(i)
            index_set.append(j)
            index_set.append(k)
for m in range(0, len(index_set), 3):
    temp_set.append(index_set[m])
    temp_set.append(index_set[m + 1])
    temp_set.append(index_set[m + 2])
    result_set.append(temp_set)
    temp_set = []
print(len(result_set))
print(result_set)
# 输出的结果:
125
[[1, 6, 11], [1, 6, 12], [1, 6, 13], [1, 6, 14], [1, 6, 15], [1, 7, 11], [1, 7, 12], [1, 7, 13],
 [1, 7, 14], [1, 7, 15], [1, 8, 11], [1, 8, 12], [1, 8, 13], [1, 8, 14], [1, 8, 15], [1, 9, 11],
 [1, 9, 12], [1, 9, 13], [1, 9, 14], [1, 9, 15], [1, 10, 11], [1, 10, 12], [1, 10, 13], [1, 10, 14],
 [1, 10, 15], [2, 6, 11], [2, 6, 12], [2, 6, 13], [2, 6, 14], [2, 6, 15], [2, 7, 11], [2, 7, 12],
 [2, 7, 13], [2, 7, 14], [2, 7, 15], [2, 8, 11], [2, 8, 12], [2, 8, 13], [2, 8, 14], [2, 8, 15], 
 [2, 9, 11], [2, 9, 12], [2, 9, 13], [2, 9, 14], [2, 9, 15], [2, 10, 11], [2, 10, 12], [2, 10, 13],
 [2, 10, 14], [2, 10, 15], [3, 6, 11], [3, 6, 12], [3, 6, 13], [3, 6, 14], [3, 6, 15], [3, 7, 11],
 [3, 7, 12], [3, 7, 13], [3, 7, 14], [3, 7, 15], [3, 8, 11], [3, 8, 12], [3, 8, 13], [3, 8, 14],
 [3, 8, 15], [3, 9, 11], [3, 9, 12], [3, 9, 13], [3, 9, 14], [3, 9, 15], [3, 10, 11], [3, 10, 12],
 [3, 10, 13], [3, 10, 14], [3, 10, 15], [4, 6, 11], [4, 6, 12], [4, 6, 13], [4, 6, 14], [4, 6, 15], 
 [4, 7, 11], [4, 7, 12], [4, 7, 13], [4, 7, 14], [4, 7, 15], [4, 8, 11], [4, 8, 12], [4, 8, 13],
 [4, 8, 14], [4, 8, 15], [4, 9, 11], [4, 9, 12], [4, 9, 13], [4, 9, 14], [4, 9, 15], [4, 10, 11],
 [4, 10, 12], [4, 10, 13], [4, 10, 14], [4, 10, 15], [5, 6, 11], [5, 6, 12], [5, 6, 13], [5, 6, 14], 
 [5, 6, 15], [5, 7, 11], [5, 7, 12], [5, 7, 13], [5, 7, 14], [5, 7, 15], [5, 8, 11], [5, 8, 12], 
 [5, 8, 13], [5, 8, 14], [5, 8, 15], [5, 9, 11], [5, 9, 12], [5, 9, 13], [5, 9, 14], [5, 9, 15],
 [5, 10, 11], [5, 10, 12], [5, 10, 13], [5, 10, 14], [5, 10, 15]]
	

你可能感兴趣的:(Python学习)