Hướng dẫn python islice - sau đó trăn

Mô -đun này thực hiện một số khối xây dựng vòng lặp lấy cảm hứng từ các cấu trúc từ APL, Haskell và SML. Mỗi người đã được thu hồi trong một hình thức phù hợp cho Python.iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python.

Mô -đun chuẩn hóa một bộ lõi của các công cụ nhanh, hiệu quả bộ nhớ hữu ích cho chính chúng hoặc kết hợp. Cùng nhau, họ tạo thành một đại số iterator của người Viking, giúp việc xây dựng các công cụ chuyên dụng một cách ngắn gọn và hiệu quả trong Python thuần túy.

Chẳng hạn, SML cung cấp một công cụ lập bảng:

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
8 tạo ra chuỗi
def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
9. Hiệu ứng tương tự có thể đạt được trong Python bằng cách kết hợp
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
0 và
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
1 để tạo thành
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
2.

Các công cụ này và các đối tác tích hợp của chúng cũng hoạt động tốt với các chức năng tốc độ cao trong mô-đun

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
3. Ví dụ, toán tử nhân có thể được ánh xạ trên hai vectơ để tạo thành một sản phẩm chấm hiệu quả:
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
4.

Vòng lặp vô hạn:

Người lặp lại

Tranh luận

Kết quả

Thí dụ

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
1

Bắt đầu, [Bước]

Bắt đầu, bắt đầu+bước, bắt đầu+2*Bước,

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
6

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
7

P

P0, P1, Plast Plast, P0, P1, Mạnh

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
8

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
9

elem [, n]

elem, elem, elem,… vô tận hoặc lên đến n lần

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
0

Trình lặp chấm dứt trên chuỗi đầu vào ngắn nhất:

Người lặp lại

Tranh luận

Kết quả

Thí dụ

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
1

Bắt đầu, [Bước]

Bắt đầu, bắt đầu+bước, bắt đầu+2*Bước,

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
2

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
3

P

P0, P1, Plast Plast, P0, P1, Mạnh

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
4

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
5

elem [, n]

P0, P1, Plast Plast, P0, P1, Mạnh

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
6

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
7

elem [, n]

elem, elem, elem,… vô tận hoặc lên đến n lần

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
8

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
9

Trình lặp chấm dứt trên chuỗi đầu vào ngắn nhất:

P [, func]

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
0

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
1

Trình lặp chấm dứt trên chuỗi đầu vào ngắn nhất:

P [, func]

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
2

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
3

P0, P0+P1, P0+P1+P2,

P, Q,…

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
4

P0, P1, Plast Plast, Q0, Q1, Mạnh

Có thể lặp lại

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
5

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
6

elem [, n]

elem, elem, elem,… vô tận hoặc lên đến n lần

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
7

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
8

Trình lặp chấm dứt trên chuỗi đầu vào ngắn nhất:

P [, func]

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
9

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
0

Trình lặp chấm dứt trên chuỗi đầu vào ngắn nhất:

P [, func]

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
1

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
2

P0, P0+P1, P0+P1+P2,

P, Q,…

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
3

P

P0, P1, Plast Plast, P0, P1, Mạnh

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
4

elem [, n]

Người lặp lại

Tranh luận

Kết quả

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
5

Thí dụ

Bắt đầu, [Bước]

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
6

Bắt đầu, bắt đầu+bước, bắt đầu+2*Bước,

P

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
7

P0, P1, Plast Plast, P0, P1, Mạnh

elem [, n]

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
8

P0, P1, Plast Plast, P0, P1, Mạnh

elem [, n]

elem, elem, elem,… vô tận hoặc lên đến n lần

Kết quả

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
9

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
0

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
1

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
2

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
3

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
4

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
5

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
6

Thí dụ

Bắt đầu, [Bước]

Bắt đầu, bắt đầu+bước, bắt đầu+2*Bước,accumulate[iterable[, func, *, initial=None]]

P

P0, P1, Plast Plast, P0, P1, Mạnh

elem [, n]

elem, elem, elem,… vô tận hoặc lên đến n lần

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

Trình lặp chấm dứt trên chuỗi đầu vào ngắn nhất:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]

P [, func]

P0, P0+P1, P0+P1+P2,

P, Q,…Added the optional func parameter.

P0, P1, Plast Plast, Q0, Q1, MạnhAdded the optional initial parameter.

Có thể lặp lạichain[*iterables]

Dữ liệu, bộ chọn

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

[d [0] nếu s [0]], [d [1] nếu s [1]], thì chain.from_iterable[iterable]

Pred, Seq

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

SEQ [n], SEQ [n+1], bắt đầu khi dự đoán không thành côngcombinations[iterable, r]

Các yếu tố của SEQ trong đó PRES [ELEM] là sai

Có thể lặp lại [, Key]

Các yếu tố được coi là duy nhất dựa trên vị trí của chúng, không phải dựa trên giá trị của chúng. Vì vậy, nếu các phần tử đầu vào là duy nhất, sẽ không có giá trị lặp lại trong mỗi kết hợp.

Tương đương với:

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]

Mã cho

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
7 cũng có thể được biểu thị dưới dạng theo sau
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
6 sau khi lọc các mục trong đó các phần tử không theo thứ tự được sắp xếp [theo vị trí của chúng trong nhóm đầu vào]:

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]

Số lượng các mặt hàng được trả lại là

def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
6 khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
7 hoặc không khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
8.

itertools.comcombinations_with_replacement[iterable, r]

Trả về r dài các phần tử của các phần tử từ đầu vào có thể cho phép các phần tử riêng lẻ được lặp lại nhiều lần.

Các bộ dữ liệu kết hợp được phát ra theo thứ tự từ vựng theo thứ tự của đầu vào có thể điều chỉnh được. Vì vậy, nếu đầu vào có thể sắp xếp, các bộ dữ liệu đầu ra sẽ được sản xuất theo thứ tự sắp xếp.

Các yếu tố được coi là duy nhất dựa trên vị trí của chúng, không phải dựa trên giá trị của chúng. Vì vậy, nếu các phần tử đầu vào là duy nhất, các kết hợp được tạo cũng sẽ là duy nhất.

Tương đương với:

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]

def combinations_with_replacement[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in product[range[n], repeat=r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]

Mã cho

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
7 cũng có thể được biểu thị dưới dạng theo sau
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
6 sau khi lọc các mục trong đó các phần tử không theo thứ tự được sắp xếp [theo vị trí của chúng trong nhóm đầu vào]:

Số lượng các mặt hàng được trả lại là

def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
6 khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
7 hoặc không khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
8.

itertools.comcompress[data, selectors]

Trả về r dài các phần tử của các phần tử từ đầu vào có thể cho phép các phần tử riêng lẻ được lặp lại nhiều lần.

Các bộ dữ liệu kết hợp được phát ra theo thứ tự từ vựng theo thứ tự của đầu vào có thể điều chỉnh được. Vì vậy, nếu đầu vào có thể sắp xếp, các bộ dữ liệu đầu ra sẽ được sản xuất theo thứ tự sắp xếp.

Số lượng các mặt hàng được trả lại là

def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
6 khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
7 hoặc không khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
8.

itertools.comcount[start=0, step=1]

Trả về r dài các phần tử của các phần tử từ đầu vào có thể cho phép các phần tử riêng lẻ được lặp lại nhiều lần.

def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step

Các bộ dữ liệu kết hợp được phát ra theo thứ tự từ vựng theo thứ tự của đầu vào có thể điều chỉnh được. Vì vậy, nếu đầu vào có thể sắp xếp, các bộ dữ liệu đầu ra sẽ được sản xuất theo thứ tự sắp xếp.

Các yếu tố được coi là duy nhất dựa trên vị trí của chúng, không phải dựa trên giá trị của chúng. Vì vậy, nếu các phần tử đầu vào là duy nhất, các kết hợp được tạo cũng sẽ là duy nhất.Added step argument and allowed non-integer arguments.

Mã cho
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
8 cũng có thể được biểu thị dưới dạng theo sau
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
5 sau khi lọc các mục trong đó các phần tử không theo thứ tự được sắp xếp [theo vị trí của chúng trong nhóm đầu vào]:
cycle[iterable]

Số lượng các mặt hàng được trả lại là

def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
1 khi
def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
2.

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
0

Mới trong phiên bản 3.1.

itertools.compress [dữ liệu, bộ chọn] ¶dropwhile[predicate, iterable]

Tạo một trình lặp lại lọc các phần tử từ dữ liệu chỉ trả về các yếu tố có phần tử tương ứng trong các bộ chọn đánh giá thành

def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
3. Dừng khi các phép lặp dữ liệu hoặc bộ chọn đã bị cạn kiệt. Tương đương với:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
1

________số 8filterfalse[predicate, iterable]

itertools.count [start = 0, bước = 1] ¶

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
2

Tạo một trình lặp lại trả về các giá trị cách đều nhau bắt đầu bằng việc bắt đầu số. Thường được sử dụng như một đối số cho
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
0 để tạo các điểm dữ liệu liên tiếp. Ngoài ra, được sử dụng với
def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
5 để thêm số thứ tự. Tương đương với:
groupby[iterable, key=None]

Khi đếm với số điểm nổi, độ chính xác tốt hơn đôi khi có thể đạt được bằng cách thay thế mã nhân như:

def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
6.

Đã thay đổi trong phiên bản 3.1: Đã thêm đối số bước và cho phép đối số không số nguyên.

itertools.cycle [có thể lặp lại] ¶

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
3

Làm cho một trình lặp trả về các phần tử từ Itable và lưu một bản sao của mỗi người. Khi có thể cạn kiệt, các yếu tố trả lại từ bản sao đã lưu. Lặp lại vô thời hạn. Tương đương với:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
4

Lưu ý, thành viên này của bộ công cụ có thể yêu cầu lưu trữ phụ trợ đáng kể [tùy thuộc vào độ dài của điều đó].islice[iterable, stop]itertools.islice[iterable, start, stop[, step]]

itertools.drop trong khi [vị ngữ, có thể lặp lại] ¶

Nếu bắt đầu là

def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
8, thì lần lặp lại bắt đầu từ 0. Nếu bước là
def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
8, thì bước mặc định là một.

Không giống như cắt thông thường,

def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
4 không hỗ trợ các giá trị âm cho bắt đầu, dừng hoặc bước. Có thể được sử dụng để trích xuất các trường liên quan từ dữ liệu trong đó cấu trúc bên trong đã được làm phẳng [ví dụ: một báo cáo đa dòng có thể liệt kê một trường tên trên mỗi dòng thứ ba].

Tương đương với:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
5

itertools.pairwise [có thể lặp lại] ¶pairwise[iterable]

Trả về các cặp chồng chéo liên tiếp được lấy từ đầu vào có thể điều chỉnh được.

Số lượng 2 bộ trong đầu ra đầu ra sẽ ít hơn số lượng đầu vào. Nó sẽ trống nếu đầu vào có thể có ít hơn hai giá trị.

Tương đương với:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
6

itertools.pairwise [có thể lặp lại] ¶

Trả về các cặp chồng chéo liên tiếp được lấy từ đầu vào có thể điều chỉnh được.permutations[iterable, r=None]

Số lượng 2 bộ trong đầu ra đầu ra sẽ ít hơn số lượng đầu vào. Nó sẽ trống nếu đầu vào có thể có ít hơn hai giá trị.

Mới trong phiên bản 3.10.

itertools.permutations [itable, r = none] ¶

Trả lại hoán vị chiều dài r liên tiếp của các phần tử trong ITBERSEBLE.

Tương đương với:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
7

itertools.pairwise [có thể lặp lại] ¶

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
8

Trả về các cặp chồng chéo liên tiếp được lấy từ đầu vào có thể điều chỉnh được.

Số lượng 2 bộ trong đầu ra đầu ra sẽ ít hơn số lượng đầu vào. Nó sẽ trống nếu đầu vào có thể có ít hơn hai giá trị.product[*iterables, repeat=1]

Mới trong phiên bản 3.10.

itertools.permutations [itable, r = none] ¶

Trả lại hoán vị chiều dài r liên tiếp của các phần tử trong ITBERSEBLE.

Nếu r không được chỉ định hoặc là

def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
8, thì r mặc định theo chiều dài của độ dài và tất cả các hoán vị có độ dài đầy đủ có thể được tạo ra.

Các bộ dữ liệu hoán vị được phát ra theo thứ tự từ vựng theo thứ tự của đầu vào có thể điều chỉnh được. Vì vậy, nếu đầu vào có thể sắp xếp, các bộ dữ liệu đầu ra sẽ được sản xuất theo thứ tự sắp xếp.

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
9

Các yếu tố được coi là duy nhất dựa trên vị trí của chúng, không phải dựa trên giá trị của chúng. Vì vậy, nếu các phần tử đầu vào là duy nhất, sẽ không có giá trị lặp lại trong một hoán vị.

Mã cho
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
6 cũng có thể được biểu thị dưới dạng phần sau của
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
5, được lọc để loại trừ các mục với các phần tử lặp lại [các phần tử từ cùng một vị trí trong nhóm đầu vào]:
repeat[object[, times]]

Số lượng các mặt hàng được trả lại là

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
12 khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
7 hoặc không khi
def compress[data, selectors]:
    # compress['ABCDEF', [1,0,1,0,1,1]] --> A C E F
    return [d for d, s in zip[data, selectors] if s]
8.

Tương đương với:

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
0

itertools.product [*iterables, lặp lại = 1] ¶

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
1

Sản phẩm Cartesian của Iterables đầu vào.starmap[function, iterable]

Gần tương đương với các vòng lặp lồng nhau trong một biểu thức máy phát. Ví dụ:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
15 trả về giống như
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
16.

Các vòng lặp lồng nhau chu kỳ giống như một đồng hồ đo với phần tử ngoài cùng bên phải trên mỗi lần lặp. Mẫu này tạo ra một thứ tự từ vựng để nếu các lần lặp đầu vào được sắp xếp, các bộ dữ liệu sản phẩm được phát ra theo thứ tự được sắp xếp.

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
2

Để tính toán sản phẩm của một điều khác nhau, chỉ định số lần lặp lại với đối số từ khóa lặp lại tùy chọn. Ví dụ,
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
17 có nghĩa là giống như
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
18.
takewhile[predicate, iterable]

Hàm này gần tương đương với mã sau, ngoại trừ việc triển khai thực tế không xây dựng kết quả trung gian trong bộ nhớ:

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
3

Trước khi
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
5 chạy, nó tiêu thụ hoàn toàn các lần lặp đầu vào, giữ các nhóm các giá trị trong bộ nhớ để tạo ra các sản phẩm. Theo đó, nó chỉ hữu ích với các đầu vào hữu hạn.
tee[iterable, n=2]

itertools.repeat [Object [, Times]] ¶

Tạo một trình lặp lại trả về đối tượng nhiều lần. Chạy vô thời hạn trừ khi các đối số thời gian được chỉ định.

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
4

Một cách sử dụng phổ biến để lặp lại là cung cấp một luồng các giá trị không đổi để ánh xạ hoặc zip:

itertools.starmap [chức năng, có thể lặp lại] ¶

Tạo một iterator tính toán chức năng bằng cách sử dụng các đối số thu được từ có thể điều chỉnh được. Được sử dụng thay vì

def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
0 khi các tham số đối số đã được nhóm lại trong các bộ dữ liệu từ một lần lặp lại [khi dữ liệu đã được giới thiệu trước khi ra mắt].

Sự khác biệt giữa
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
0 và
def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
8 song song với sự khác biệt giữa
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
23 và
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
24. Tương đương với:
zip_longest[*iterables, fillvalue=None]

itertools.takewhile [vị ngữ, có thể lặp lại] ¶

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
5

Nếu một trong các vòng lặp có khả năng là vô hạn, thì hàm

def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
3 nên được gói bằng một cái gì đó giới hạn số lượng cuộc gọi [ví dụ
def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
4 hoặc
def combinations_with_replacement[iterable, r]:
    # combinations_with_replacement['ABC', 2] --> AA AB AC BB BC CC
    pool = tuple[iterable]
    n = len[pool]
    if not n and r:
        return
    indices = [0] * r
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * [r - i]
        yield tuple[pool[i] for i in indices]
0]. Nếu không được chỉ định, FillValue mặc định là
def count[start=0, step=1]:
    # count[10] --> 10 11 12 13 14 ...
    # count[2.5, 0.5] --> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step
8.

Công thức nấu ăn itertools

Phần này hiển thị các công thức nấu ăn để tạo một bộ công cụ mở rộng bằng cách sử dụng các itertools hiện có làm khối xây dựng.

Mục đích chính của các công thức nấu ăn là giáo dục. Các công thức nấu ăn cho thấy nhiều cách suy nghĩ khác nhau về các công cụ riêng lẻ - ví dụ,

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
35 có liên quan đến khái niệm làm phẳng. Các công thức nấu ăn cũng đưa ra ý tưởng về các cách mà các công cụ có thể được kết hợp - ví dụ: làm thế nào
def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
7 và
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
37 có thể làm việc cùng nhau. Các công thức nấu ăn cũng hiển thị các mẫu để sử dụng itertools với các mô-đun
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
3 và
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
39 cũng như với các itertools tích hợp như
def from_iterable[iterables]:
    # chain.from_iterable[['ABC', 'DEF']] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
0,
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
41,
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
42 và
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
43.

Mục đích thứ yếu của các công thức nấu ăn là phục vụ như một vườn ươm. ITERTOOLS

def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
1,
def combinations[iterable, r]:
    # combinations['ABCD', 2] --> AB AC AD BC BD CD
    # combinations[range[4], 3] --> 012 013 023 123
    pool = tuple[iterable]
    n = len[pool]
    if r > n:
        return
    indices = list[range[r]]
    yield tuple[pool[i] for i in indices]
    while True:
        for i in reversed[range[r]]:
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range[i+1, r]:
            indices[j] = indices[j-1] + 1
        yield tuple[pool[i] for i in indices]
7 và
def combinations[iterable, r]:
    pool = tuple[iterable]
    n = len[pool]
    for indices in permutations[range[n], r]:
        if sorted[indices] == list[indices]:
            yield tuple[pool[i] for i in indices]
6 bắt đầu dưới dạng công thức nấu ăn. Hiện tại, công thức
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list[accumulate[data, operator.mul]]     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list[accumulate[data, max]]              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list[accumulate[cashflows, lambda bal, pmt: bal*1.05 + pmt]]
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
47 đang được thử nghiệm để xem liệu nó có chứng minh được giá trị của nó hay không.

Về cơ bản, tất cả các công thức nấu ăn này và nhiều, nhiều công thức khác có thể được cài đặt từ dự án nhiều hơn được tìm thấy trên Chỉ số gói Python:

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
6

Nhiều công thức nấu ăn cung cấp hiệu suất cao giống như bộ công cụ cơ bản. Hiệu suất bộ nhớ vượt trội được giữ bằng cách xử lý các yếu tố một lần thay vì đưa toàn bộ bộ nhớ vào bộ nhớ cùng một lúc. Khối lượng mã được giữ nhỏ bằng cách liên kết các công cụ với nhau theo kiểu chức năng giúp loại bỏ các biến tạm thời. Tốc độ cao được giữ lại bằng cách ưu tiên các khối xây dựng vectơ hóa của Vectơ hơn việc sử dụng các vòng lặp và máy phát điện phải chịu chi phí thông dịch.generators which incur interpreter overhead.

def chain[*iterables]:
    # chain['ABC', 'DEF'] --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
7

Bài Viết Liên Quan

Chủ Đề