• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python iterables.partitions函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sympy.utilities.iterables.partitions函数的典型用法代码示例。如果您正苦于以下问题:Python partitions函数的具体用法?Python partitions怎么用?Python partitions使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了partitions函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_partitions

def test_partitions():
    assert [p.copy() for p in partitions(6, k=2)] == [
        {2: 3}, {1: 2, 2: 2}, {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(6, k=3)] == [
        {3: 2}, {1: 1, 2: 1, 3: 1}, {1: 3, 3: 1}, {2: 3}, {1: 2, 2: 2},
        {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(6, k=2, m=2)] == []

    assert [p.copy() for p in partitions(8, k=4, m=3)] == [
        {4: 2}, {1: 1, 3: 1, 4: 1}, {2: 2, 4: 1}, {2: 1, 3: 2}] == [
        i.copy() for i in partitions(8, k=4, m=3) if all(k <= 4 for k in i)
        and sum(i.values()) <=3]

    assert [p.copy() for p in partitions(S(3), m=2)] == [
        {3: 1}, {1: 1, 2: 1}]

    assert [i.copy() for i in partitions(4, k=3)] == [
        {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}] == [
        i.copy() for i in partitions(4) if all(k <= 3 for k in i)]

    raises(ValueError, lambda: list(partitions(3, 0)))

    # Consistency check on output of _partitions and RGS_unrank.
    # This provides a sanity test on both routines.  Also verifies that
    # the total number of partitions is the same in each case.
    #    (from pkrathmann2)

    for n in range(2, 6):
        i  = 0
        for m, q  in _set_partitions(n):
            assert  q == RGS_unrank(i, n)
            i = i+1
        assert i == RGS_enum(n)
开发者ID:Krastanov,项目名称:sympy,代码行数:35,代码来源:test_iterables.py


示例2: test_partitions

def test_partitions():
    assert [p.copy() for p in partitions(6, k=2)] == [{2: 3}, \
    {1: 2, 2: 2}, {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(6, k=3)] == [{3: 2}, \
    {1: 1, 2: 1, 3: 1}, {1: 3, 3: 1}, {2: 3}, {1: 2, 2: 2}, \
    {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(6, k=2, m=2)] == []

    assert [p.copy() for p in partitions(8, k=4, m=3)] == [{4: 2},\
    {1: 1, 3: 1, 4: 1}, {2: 2, 4: 1}, {2: 1, 3: 2}]
开发者ID:ArchKaine,项目名称:sympy,代码行数:12,代码来源:test_iterables.py


示例3: test_partitions

def test_partitions():
    assert [p.copy() for p in partitions(6, k=2)] == [{2: 3}, \
    {1: 2, 2: 2}, {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(6, k=3)] == [{3: 2}, \
    {1: 1, 2: 1, 3: 1}, {1: 3, 3: 1}, {2: 3}, {1: 2, 2: 2}, \
    {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(6, k=2, m=2)] == []

    assert [p.copy() for p in partitions(8, k=4, m=3)] == [{4: 2},\
    {1: 1, 3: 1, 4: 1}, {2: 2, 4: 1}, {2: 1, 3: 2}]

    assert [p.copy() for p in partitions(S(3), 2)] == \
    [{3: 1}, {1: 1, 2: 1}]

    raises(ValueError, 'list(partitions(3, 0))')
开发者ID:BDGLunde,项目名称:sympy,代码行数:17,代码来源:test_iterables.py


示例4: test_uniq

def test_uniq():
    assert list(uniq(p.copy() for p in partitions(4))) == [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}]
    assert list(uniq(x % 2 for x in range(5))) == [0, 1]
    assert list(uniq("a")) == ["a"]
    assert list(uniq("ababc")) == list("abc")
    assert list(uniq([[1], [2, 1], [1]])) == [[1], [2, 1]]
    assert list(uniq(permutations(i for i in [[1], 2, 2]))) == [([1], 2, 2), (2, [1], 2), (2, 2, [1])]
    assert list(uniq([2, 3, 2, 4, [2], [1], [2], [3], [1]])) == [2, 3, 4, [2], [1], [3]]
开发者ID:rae1,项目名称:sympy,代码行数:8,代码来源:test_iterables.py


示例5: test_uniq

def test_uniq():
    assert list(uniq(p.copy() for p in partitions(4))) == \
        [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}]
    assert list(uniq(x % 2 for x in range(5))) == [0, 1]
    assert list(uniq('a')) == ['a']
    assert list(uniq('ababc')) == list('abc')
    assert list(uniq([[1], [2, 1], [1]])) == [[1], [2, 1], [1]]
    assert list(uniq(permutations(i for i in [[1], 2, 2]))) == \
        [([1], 2, 2), (2, [1], 2), (2, 2, [1]), (2, [1], 2), (2, 2, [1])]
开发者ID:Acebulf,项目名称:sympy,代码行数:9,代码来源:test_iterables.py


示例6: test_integer_partition

def test_integer_partition():
    # no zeros in partition
    raises(ValueError, lambda: IntegerPartition(list(range(3))))
    # check fails since 1 + 2 != 100
    raises(ValueError, lambda: IntegerPartition(100, list(range(1, 3))))
    a = IntegerPartition(8, [1, 3, 4])
    b = a.next_lex()
    c = IntegerPartition([1, 3, 4])
    d = IntegerPartition(8, {1: 3, 3: 1, 2: 1})
    assert a == c
    assert a.integer == d.integer
    assert a.conjugate == [3, 2, 2, 1]
    assert (a == b) is False
    assert a <= b
    assert (a > b) is False
    assert a != b

    for i in range(1, 11):
        next = set()
        prev = set()
        a = IntegerPartition([i])
        ans = set([IntegerPartition(p) for p in partitions(i)])
        n = len(ans)
        for j in range(n):
            next.add(a)
            a = a.next_lex()
            IntegerPartition(i, a.partition)  # check it by giving i
        for j in range(n):
            prev.add(a)
            a = a.prev_lex()
            IntegerPartition(i, a.partition)  # check it by giving i
        assert next == ans
        assert prev == ans

    assert IntegerPartition([1, 2, 3]).as_ferrers() == '###\n##\n#'
    assert IntegerPartition([1, 1, 3]).as_ferrers('o') == 'ooo\no\no'
    assert str(IntegerPartition([1, 1, 3])) == '[3, 1, 1]'
    assert IntegerPartition([1, 1, 3]).partition == [3, 1, 1]

    raises(ValueError, lambda: random_integer_partition(-1))
    assert random_integer_partition(1) == [1]
    assert random_integer_partition(10, seed=[1, 3, 2, 1, 5, 1]
            ) == [5, 2, 1, 1, 1]
开发者ID:AdrianPotter,项目名称:sympy,代码行数:43,代码来源:test_partitions.py


示例7: test_nC_nP_nT

def test_nC_nP_nT():
    from sympy.utilities.iterables import (
        multiset_permutations, multiset_combinations, multiset_partitions,
        partitions, subsets, permutations)
    from sympy.functions.combinatorial.numbers import (
        nP, nC, nT, stirling, _multiset_histogram, _AOP_product)
    from sympy.combinatorics.permutations import Permutation
    from sympy.core.numbers import oo
    from random import choice

    c = string.ascii_lowercase
    for i in range(100):
        s = ''.join(choice(c) for i in range(7))
        u = len(s) == len(set(s))
        try:
            tot = 0
            for i in range(8):
                check = nP(s, i)
                tot += check
                assert len(list(multiset_permutations(s, i))) == check
                if u:
                    assert nP(len(s), i) == check
            assert nP(s) == tot
        except AssertionError:
            print(s, i, 'failed perm test')
            raise ValueError()

    for i in range(100):
        s = ''.join(choice(c) for i in range(7))
        u = len(s) == len(set(s))
        try:
            tot = 0
            for i in range(8):
                check = nC(s, i)
                tot += check
                assert len(list(multiset_combinations(s, i))) == check
                if u:
                    assert nC(len(s), i) == check
            assert nC(s) == tot
            if u:
                assert nC(len(s)) == tot
        except AssertionError:
            print(s, i, 'failed combo test')
            raise ValueError()

    for i in range(1, 10):
        tot = 0
        for j in range(1, i + 2):
            check = nT(i, j)
            tot += check
            assert sum(1 for p in partitions(i, j, size=True) if p[0] == j) == check
        assert nT(i) == tot

    for i in range(1, 10):
        tot = 0
        for j in range(1, i + 2):
            check = nT(range(i), j)
            tot += check
            assert len(list(multiset_partitions(range(i), j))) == check
        assert nT(range(i)) == tot

    for i in range(100):
        s = ''.join(choice(c) for i in range(7))
        u = len(s) == len(set(s))
        try:
            tot = 0
            for i in range(1, 8):
                check = nT(s, i)
                tot += check
                assert len(list(multiset_partitions(s, i))) == check
                if u:
                    assert nT(range(len(s)), i) == check
            if u:
                assert nT(range(len(s))) == tot
            assert nT(s) == tot
        except AssertionError:
            print(s, i, 'failed partition test')
            raise ValueError()

    # tests for Stirling numbers of the first kind that are not tested in the
    # above
    assert [stirling(9, i, kind=1) for i in range(11)] == [
        0, 40320, 109584, 118124, 67284, 22449, 4536, 546, 36, 1, 0]
    perms = list(permutations(range(4)))
    assert [sum(1 for p in perms if Permutation(p).cycles == i)
            for i in range(5)] == [0, 6, 11, 6, 1] == [
            stirling(4, i, kind=1) for i in range(5)]
    # http://oeis.org/A008275
    assert [stirling(n, k, signed=1)
        for n in range(10) for k in range(1, n + 1)] == [
            1, -1,
            1, 2, -3,
            1, -6, 11, -6,
            1, 24, -50, 35, -10,
            1, -120, 274, -225, 85, -15,
            1, 720, -1764, 1624, -735, 175, -21,
            1, -5040, 13068, -13132, 6769, -1960, 322, -28,
            1, 40320, -109584, 118124, -67284, 22449, -4536, 546, -36, 1]
    # http://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind
    assert  [stirling(n, k, kind=1)
#.........这里部分代码省略.........
开发者ID:B-Rich,项目名称:sympy,代码行数:101,代码来源:test_comb_numbers.py


示例8: test_F6

def test_F6():
    partTest = [p.copy() for p in partitions(4)]
    partDesired = [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2:1}, {1: 4}]
    assert partTest == partDesired
开发者ID:batya239,项目名称:sympy,代码行数:4,代码来源:test_wester.py


示例9: test_partitions

def test_partitions():
    ans = [[{}], [(0, {})]]
    for i in range(2):
        assert list(partitions(0, size=i)) == ans[i]
        assert list(partitions(1, 0, size=i)) == ans[i]
        assert list(partitions(6, 2, 2, size=i)) == ans[i]
        assert list(partitions(6, 2, None, size=i)) != ans[i]
        assert list(partitions(6, None, 2, size=i)) != ans[i]
        assert list(partitions(6, 2, 0, size=i)) == ans[i]

    assert [p.copy() for p in partitions(6, k=2)] == [
        {2: 3}, {1: 2, 2: 2}, {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(6, k=3)] == [
        {3: 2}, {1: 1, 2: 1, 3: 1}, {1: 3, 3: 1}, {2: 3}, {1: 2, 2: 2},
        {1: 4, 2: 1}, {1: 6}]

    assert [p.copy() for p in partitions(8, k=4, m=3)] == [
        {4: 2}, {1: 1, 3: 1, 4: 1}, {2: 2, 4: 1}, {2: 1, 3: 2}] == [
        i.copy() for i in partitions(8, k=4, m=3) if all(k <= 4 for k in i)
        and sum(i.values()) <=3]

    assert [p.copy() for p in partitions(S(3), m=2)] == [
        {3: 1}, {1: 1, 2: 1}]

    assert [i.copy() for i in partitions(4, k=3)] == [
        {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}] == [
        i.copy() for i in partitions(4) if all(k <= 3 for k in i)]


    # Consistency check on output of _partitions and RGS_unrank.
    # This provides a sanity test on both routines.  Also verifies that
    # the total number of partitions is the same in each case.
    #    (from pkrathmann2)

    for n in range(2, 6):
        i  = 0
        for m, q  in _set_partitions(n):
            assert  q == RGS_unrank(i, n)
            i += 1
        assert i == RGS_enum(n)
开发者ID:asmeurer,项目名称:sympy,代码行数:41,代码来源:test_iterables.py


示例10: test_uniq

def test_uniq():
    assert list(uniq(p.copy() for p in partitions(4))) == [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}]
    assert list(uniq(x % 2 for x in range(5))) == [0, 1]
    assert list(uniq("a")) == ["a"]
    assert list(uniq("ababc")) == list("abc")
开发者ID:skolwind,项目名称:sympy,代码行数:5,代码来源:test_iterables.py


示例11: kbin

def kbin(l, k, ordered=True):
    """
    Return sequence ``l`` partitioned into ``k`` bins.
    If ordered is True then the order of the items in the
    flattened partition will be the same as the order of the
    items in ``l``; if False, all permutations of the items will
    be given; if None, only unique permutations for a given
    partition will be given.

    Examples
    ========

    >>> from sympy.utilities.iterables import kbin
    >>> for p in kbin(range(3), 2):
    ...     print p
    ...
    [[0], [1, 2]]
    [[0, 1], [2]]
    >>> for p in kbin(range(3), 2, ordered=False):
    ...     print p
    ...
    [(0,), (1, 2)]
    [(0,), (2, 1)]
    [(1,), (0, 2)]
    [(1,), (2, 0)]
    [(2,), (0, 1)]
    [(2,), (1, 0)]
    [(0, 1), (2,)]
    [(0, 2), (1,)]
    [(1, 0), (2,)]
    [(1, 2), (0,)]
    [(2, 0), (1,)]
    [(2, 1), (0,)]
    >>> for p in kbin(range(3), 2, ordered=None):
    ...     print p
    ...
    [[0], [1, 2]]
    [[1], [2, 0]]
    [[2], [0, 1]]
    [[0, 1], [2]]
    [[1, 2], [0]]
    [[2, 0], [1]]

    """
    from sympy.utilities.iterables import partitions
    from itertools import permutations
    def rotations(seq):
        for i in range(len(seq)):
            yield seq
            seq.append(seq.pop(0))
    if ordered is None:
        func = rotations
    else:
        func = permutations
    for p in partitions(len(l), k):
        if sum(p.values()) != k:
            continue
        for pe in permutations(p.keys()):
            rv = []
            i = 0
            for part in pe:
                for do in range(p[part]):
                    j = i + part
                    rv.append(l[i: j])
                    i = j
            if ordered:
                yield rv
            else:
                template = [len(i) for i in rv]
                for pp in func(l):
                    rvp = []
                    ii = 0
                    for t in template:
                        jj = ii + t
                        rvp.append(pp[ii: jj])
                        ii = jj
                    yield rvp
开发者ID:mrocklin,项目名称:unify,代码行数:77,代码来源:unify.py


示例12: test_uniq

def test_uniq():
    assert list(uniq(p.copy() for p in partitions(4))) == \
        [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}]
    assert list(uniq(x % 2 for x in range(5))) == [0, 1]
    assert list(uniq('a')) == ['a']
    assert list(uniq('ababc')) == list('abc')
开发者ID:FireJade,项目名称:sympy,代码行数:6,代码来源:test_iterables.py



注:本文中的sympy.utilities.iterables.partitions函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python iterables.postorder_traversal函数代码示例发布时间:2022-05-27
下一篇:
Python iterables.ordered函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap