Python3 my tips. itertools, combinations_with_replacement()

Python3 コード

iter_c_w_r.py

#!/usr/bin/env python3


"""(docstring)
"""


import itertools
import math
import statistics


def iter_c_w_r(data):
    """(docstring)
    """

    for i in itertools.combinations_with_replacement(data, 3):
        print(i)
        print('unpack: ', *i)
        print('round(math.fsum(), 2): ', round(math.fsum(i), 2))
        print('round(statistics.mean(), 2): ', round(statistics.mean(i), 2))
        print('')

if __name__ == '__main__':
    print('int:')
    data1 = [1, 2, 3]
    iter_c_w_r(data1)
    print('')

    print('float:')
    data2 = [1.1, 2.2, 3.3]
    iter_c_w_r(data2)

出力

$ python3 iter_c_w_r.py
int:
(1, 1, 1)
unpack:  1 1 1
round(math.fsum(), 2):  3.0
round(statistics.mean(), 2):  1

(1, 1, 2)
unpack:  1 1 2
round(math.fsum(), 2):  4.0
round(statistics.mean(), 2):  1.33

(1, 1, 3)
unpack:  1 1 3
round(math.fsum(), 2):  5.0
round(statistics.mean(), 2):  1.67

(1, 2, 2)
unpack:  1 2 2
round(math.fsum(), 2):  5.0
round(statistics.mean(), 2):  1.67

(1, 2, 3)
unpack:  1 2 3
round(math.fsum(), 2):  6.0
round(statistics.mean(), 2):  2

(1, 3, 3)
unpack:  1 3 3
round(math.fsum(), 2):  7.0
round(statistics.mean(), 2):  2.33

(2, 2, 2)
unpack:  2 2 2
round(math.fsum(), 2):  6.0
round(statistics.mean(), 2):  2

(2, 2, 3)
unpack:  2 2 3
round(math.fsum(), 2):  7.0
round(statistics.mean(), 2):  2.33

(2, 3, 3)
unpack:  2 3 3
round(math.fsum(), 2):  8.0
round(statistics.mean(), 2):  2.67

(3, 3, 3)
unpack:  3 3 3
round(math.fsum(), 2):  9.0
round(statistics.mean(), 2):  3


float:
(1.1, 1.1, 1.1)
unpack:  1.1 1.1 1.1
round(math.fsum(), 2):  3.3
round(statistics.mean(), 2):  1.1

(1.1, 1.1, 2.2)
unpack:  1.1 1.1 2.2
round(math.fsum(), 2):  4.4
round(statistics.mean(), 2):  1.47

(1.1, 1.1, 3.3)
unpack:  1.1 1.1 3.3
round(math.fsum(), 2):  5.5
round(statistics.mean(), 2):  1.83

(1.1, 2.2, 2.2)
unpack:  1.1 2.2 2.2
round(math.fsum(), 2):  5.5
round(statistics.mean(), 2):  1.83

(1.1, 2.2, 3.3)
unpack:  1.1 2.2 3.3
round(math.fsum(), 2):  6.6
round(statistics.mean(), 2):  2.2

(1.1, 3.3, 3.3)
unpack:  1.1 3.3 3.3
round(math.fsum(), 2):  7.7
round(statistics.mean(), 2):  2.57

(2.2, 2.2, 2.2)
unpack:  2.2 2.2 2.2
round(math.fsum(), 2):  6.6
round(statistics.mean(), 2):  2.2

(2.2, 2.2, 3.3)
unpack:  2.2 2.2 3.3
round(math.fsum(), 2):  7.7
round(statistics.mean(), 2):  2.57

(2.2, 3.3, 3.3)
unpack:  2.2 3.3 3.3
round(math.fsum(), 2):  8.8
round(statistics.mean(), 2):  2.93

(3.3, 3.3, 3.3)
unpack:  3.3 3.3 3.3
round(math.fsum(), 2):  9.9
round(statistics.mean(), 2):  3.3