41202
pythonにて  
S=0,1,2,....,a(b+1)までのS(0,p,p+a*q)を除いての組み合わせで、このSから2つ取り出して列挙していく(a=4,b=2,p=1,q=1)やり方でこのようにプログラミングが書けると教えていただいたんですが↓↓

```

    >>> import itertools
    >>> a = 4
    >>> b = 2
    >>> p = 1
    >>> q = 1
    >>> s =[i for i in range(0, m*(l+1)+1) if i not in (0, p, p+m*q)]
    n=list(itertools.combinations(s,2))
    print n
    
    [(2, 3), (2, 4), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (3, 4), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (4, 11), (4, 12), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (8, 9), (8, 10), (8, 11), (8, 12), (9, 10), (9, 11), (9, 12), (10, 11), (10, 12), (11, 12)]
    
```

今回は、2個取りだすと指定せずに1～10個取り出したものをすべて列挙して表示させる方法を教えていただきたいです。

よろしくお願いします。
CC BY-SA 3.0
2018-01-25T04:31:25.927
41194
2018-01-25T07:15:05.397
2018-01-25T04:57:23.240
27045
27045
question
python
リストから組み合わせで列挙する方法
繰り返しの構文を使えば書くことができます。

まず、リスト `s` から `i` 個取り出す組み合わせをつくるためにはどのように書けばよいか考えます。これは `i` の値が分かっていれば、

```

    list(itertools.combinations(s, i))
    
```

のように書けます。この `i` の値を 1 から 10 まで順番に変えながら表示すればよいわけなので、`for`
文を使った繰り返し構文の出番です。以下のように書けます。

```

    $ python3
    >>> a = 4
    >>> b = 2
    >>> p = 1
    >>> q = 1
    >>> s = [i for i in range(0, a * (b + 1) + 1) if i not in [0, p, p + a * q]]
    >>> for i in range(1, 11):
    ...   print(list(itertools.combinations(s, i)))
    ...
    
```

* * *

ここからおまけです: 単に出力するだけだとたくさん文字が出てきて何だか分からないので、組み合わせのリストを返す関数として定義してみました。

```

    $ python3
    >>> import itertools
    >>> def combs(a, b, p, q):
    ...   s = [i for i in range(0, a * (b + 1) + 1) if i not in [0, p, p + a * q]]
    ...   l = []
    ...   for i in range(1, 11):
    ...     l.append(list(itertools.combinations(s, i)))
    ...   return l
    ...
    >>> cs = combs(4, 2, 1, 1)
    >>> cs[1]       # 2 つ取る組み合わせ
    [(2, 3), (2, 4), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (3, 4), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (4, 11), (4, 12), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (8, 9), (8, 10), (8, 11), (8, 12), (9, 10), (9, 11), (9, 12), (10, 11), (10, 12), (11, 12)]
    >>> cs[2]       # 3 つ取る組み合わせ
    [(2, 3, 4), (2, 3, 6), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 3, 10), (2, 3, 11), (2, 3, 12), (2, 4, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 4, 10), (2, 4, 11), (2, 4, 12), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 6, 10), (2, 6, 11), (2, 6, 12), (2, 7, 8), (2, 7, 9), (2, 7, 10), (2, 7, 11), (2, 7, 12), (2, 8, 9), (2, 8, 10), (2, 8, 11), (2, 8, 12), (2, 9, 10), (2, 9, 11), (2, 9, 12), (2, 10, 11), (2, 10, 12), (2, 11, 12), (3, 4, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 4, 10), (3, 4, 11), (3, 4, 12), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 6, 10), (3, 6, 11), (3, 6, 12), (3, 7, 8), (3, 7, 9), (3, 7, 10), (3, 7, 11), (3, 7, 12), (3, 8, 9), (3, 8, 10), (3, 8, 11), (3, 8, 12), (3, 9, 10), (3, 9, 11), (3, 9, 12), (3, 10, 11), (3, 10, 12), (3, 11, 12), (4, 6, 7), (4, 6, 8), (4, 6, 9), (4, 6, 10), (4, 6, 11), (4, 6, 12), (4, 7, 8), (4, 7, 9), (4, 7, 10), (4, 7, 11), (4, 7, 12), (4, 8, 9), (4, 8, 10), (4, 8, 11), (4, 8, 12), (4, 9, 10), (4, 9, 11), (4, 9, 12), (4, 10, 11), (4, 10, 12), (4, 11, 12), (6, 7, 8), (6, 7, 9), (6, 7, 10), (6, 7, 11), (6, 7, 12), (6, 8, 9), (6, 8, 10), (6, 8, 11), (6, 8, 12), (6, 9, 10), (6, 9, 11), (6, 9, 12), (6, 10, 11), (6, 10, 12), (6, 11, 12), (7, 8, 9), (7, 8, 10), (7, 8, 11), (7, 8, 12), (7, 9, 10), (7, 9, 11), (7, 9, 12), (7, 10, 11), (7, 10, 12), (7, 11, 12), (8, 9, 10), (8, 9, 11), (8, 9, 12), (8, 10, 11), (8, 10, 12), (8, 11, 12), (9, 10, 11), (9, 10, 12), (9, 11, 12), (10, 11, 12)]
    >>> len(cs[3])  # 4 つとる組み合わせの数
    210
    
```
CC BY-SA 3.0
2018-01-25T07:15:05.397
41202
2018-01-25T07:15:05.397
19110
41194
answer