0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonの便利な関数を3つ紹介!組み合わせや順列の計算を簡単に行いたい方必見

Posted at

今回はpythonで組み合わせや順列の計算を行う際に便利な関数を紹介します。
しかし、プログラミング初心者であるため、内容に誤りがあるかもしれません。
誤りがあれば、どんどん指摘してください。

combinations関数

組み合わせを列挙する際に便利な関数です。下記のコードは、3枚の数字カード(0,1,2)から2枚引いた時に取れるカードの組み合わせを出力します。

コード

    import itertools
    list(itertools.combinations(range(3),2))

結果

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

permutaitons関数

順列を列挙する際に便利な関数です。下記は、3枚の数字カード(0,1,2)の並び順のパターンを出力します。
コード

    import itertools
    list(itertools.permutaitons(range(3)))

結果

    [(0, 1, 2),  (0, 2, 1),( 1, 0, 2), ( 1, 2, 0), ( 2, 0, 1), ( 2, 1, 0)]   #6パターン

join関数

リストの中身を連結する際に便利な関数です。下記では、testリストの文字列を連結しています。

コード

    test = ['ab', 'c', 'def']
    result = ''.join(test)
    print(result)

結果

    abcdef
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?