“Kombinasi di Python tanpa iTertools” Kode Jawaban

Apa yang dilakukan kombinasi IterTools di Python

combinations(iterable, r) : It return r-length tuples in sorted order with no repeated elements. For Example, combinations('ABCD', 2) ==> [AB, AC, AD, BC, BD, CD].
The anime coder

Kombinasi di Python tanpa iTertools

#One method that I know works, is the following:

def accumulate(iterable, func=operator.add, *, initial=None):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], initial=100) --> 100 101 103 106 110 115
    # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = iter(iterable)
    total = initial
    if initial is None:
        try:
            total = next(it)
        except StopIteration:
            return
    yield total
    for element in it:
        total = func(total, element)
        yield total
Imaginathan

Jawaban yang mirip dengan “Kombinasi di Python tanpa iTertools”

Pertanyaan yang mirip dengan “Kombinasi di Python tanpa iTertools”

Lebih banyak jawaban terkait untuk “Kombinasi di Python tanpa iTertools” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya