使用可变数量的for循环创建元组 [英] Creating tuples using a variable number of for loops

查看:58
本文介绍了使用可变数量的for循环创建元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出 n k ,我需要创建所有长度为 k 的元组,其元组来自 range(n)(0到n-1),以使元组的条目按字典顺序排列,并以特定格式包含括号.具体来说,元组从内到外在每对周围都有括号.

Given n and k, I need to create all tuples of length k whose entries are from range(n) (0 to n-1) such that the tuple's entries are in dictionary order and there are parentheses in a particular format. Specifically, the tuple has parentheses around each pair, from inside out.

例如,如果 n = 3 k = 4 ,那么我希望输出包含类似((((0,0),1),2),但不是类似(((0,0),2),1).

For example, if n=3 and k=4, then I would like the output to include something like (((0,0),1),2), but not something like (((0,0),2),1).

以下代码适用于此特定实例.问题是我不知道如何概括 k ,这是下面代码中 for 个循环的个数.我只能针对特定的 k (例如此处的 k = 4 )执行此操作.我真的需要能够对 k any 值执行此操作.

The code below works for this specific instance. The issue is that I don't know how to generalize on k, which is the number of for loops in the code below. I can only do this for a specific k, like k=4 here. I really need to be able to do this for any value of k.

n=3
k=4
my_list = []
for a in range(n):
    x = a
    for b in range(a,n):
        y = (x,b)
        for c in range(b,n):
            z = (y,c)
            for d in range(c,n):
                w = (z,d)
                my_list.append(w)
print my_list

输出:

[((((0,0),0),0),(((0,0),0),1),((((0,0),0),2),((((0,0),1),1),(((0,0),1),2),(((0,0),2),2),(((0,1),1),1),((((0,1),1),2),((((0,1),2),2),((((0,2),2),2),(((1,1),1),1),((((1,1),1),2),(((1,1),2),2),(((1,2),2),2),((((2,2),2),2)]

推荐答案

尝试一下,希望它是相关的:

Try this out, I hope it is relevant:

n=4
k=4
my_list=[(((a,b),c),d) for a in range(n) for b in range(a,n) for c in range(b,n) for d in range(c,n) ]
print my_list

输出:

[(((0,0),0),0),(((0,0),0),1),(((0,0),0),2),(((0,0),0),3),((((0,0),1),1),(((0,0),1),2),(((0,0),1),3),(((0),2),2),((((0,0),2),3),((((0,0),3),3),((((0,1),1),1),(((0,1),1),2),((((0,1),1),3),((((0,1),2),2),((((0,1),2),3),((((0,1),3),3),(((0,2),2),2),(((0,2),2),3),(((0,2),3),3),((((0,3),3),3),((((1,1),1),1),(((1,1),1),2),(((1),1),3),((((1,1),2),2),((((1,1),2),3),((((1,1),3),3),(((1,2),2),2),((((1,2),2),3),((((1,2),3),3),((((1,3),3),3),((((2,2),2),2),(((2,2),2),3),(((2,2),3),3),(((2,3),3),3),((((3,3),3),3)]

[(((0, 0), 0), 0), (((0, 0), 0), 1), (((0, 0), 0), 2), (((0, 0), 0), 3), (((0, 0), 1), 1), (((0, 0), 1), 2), (((0, 0), 1), 3), (((0, 0), 2), 2), (((0, 0), 2), 3), (((0, 0), 3), 3), (((0, 1), 1), 1), (((0, 1), 1), 2), (((0, 1), 1), 3), (((0, 1), 2), 2), (((0, 1), 2), 3), (((0, 1), 3), 3), (((0, 2), 2), 2), (((0, 2), 2), 3), (((0, 2), 3), 3), (((0, 3), 3), 3), (((1, 1), 1), 1), (((1, 1), 1), 2), (((1, 1), 1), 3), (((1, 1), 2), 2), (((1, 1), 2), 3), (((1, 1), 3), 3), (((1, 2), 2), 2), (((1, 2), 2), 3), (((1, 2), 3), 3), (((1, 3), 3), 3), (((2, 2), 2), 2), (((2, 2), 2), 3), (((2, 2), 3), 3), (((2, 3), 3), 3), (((3, 3), 3), 3)]

这篇关于使用可变数量的for循环创建元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆