程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了列出满足几个条件的组合大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决列出满足几个条件的组合?

开发过程中遇到列出满足几个条件的组合的问题如何解决?下面主要结合日常开发的经验,给出你关于列出满足几个条件的组合的解决方法建议,希望对你解决列出满足几个条件的组合有所启发或帮助;

数据:

        Items category  Value
        apple   Fruits      2
       banana   Fruits      2
       grapes   Fruits      4
         Abhi    names      5
        Ayush    names      4
        Krish    names      3
       Kartik    names      4
        Paras    names      3
       School    Place      5
   Playground    Place      4
    Courtyard    Place      1
         Farm    Place      4
        Shirt  Clothes      6
         Pant  Clothes      4
        jeans  Clothes      1
        Socks  Clothes      2
        Shoes  Clothes      1
 HandkerchIEf  Clothes      1
       Jacket  Clothes      3
      T-Shirt  Clothes      4

代码:

import pandas as pd
from itertools import permutations,combinations

Grouped = data.groupby(["category"])
Fruits = Grouped.get_group('Fruits')
names = Grouped.get_group('names')
Place = Grouped.get_group('Place')
Clothes = Grouped.get_group('Clothes')

all = combinations([Fruits,names,Place,Clothes],6)

for i in all:
    print (i)

我只想要 6 项来自所有类别(FruitsnamesPlaceClothes)的可能组合以及总数这 6 件商品的价值不得超过 15

此外,组合必须包含:

Items    Minimum & Maximum

Fruits  = 1->3
names   = 2->3
Place   = 1->2
Clothes = 2->4

解决方法

这非常适合itertools。我们将使用 combine() 生成所需长度的所有可能组合,然后使用 filterfalse() 来选择那些 满足条件。

使用问题中的数据,我们在 38,760 种可能的组合中找到了 32 种满足条件的组合。

一个复杂的问题,可能是问题中的部分程序不打印任何组合的原因,是 pandas 数据帧不会以直接的方式迭代,因为数据帧实际上是结构化对象。下面的解决方案使用 df.values 获取一个 numpy 项数组(每个项的列表长度为 3)作为组合()的输入。

来自问题的样本数据集可以用作字符串输入:

datastr = """
        Items Category  Value
        apple   Fruits      2
       banana   Fruits      2
       grapes   Fruits      4
         Abhi    Names      5
        Ayush    Names      4
        Krish    Names      3
       Kartik    Names      4
        Paras    Names      3
       School    Place      5
   Playground    Place      4
    Courtyard    Place      1
         Farm    Place      4
        Shirt  Clothes      6
         Pant  Clothes      4
        jeans  Clothes      1
        Socks  Clothes      2
        Shoes  Clothes      1
 Handkerchief  Clothes      1
       Jacket  Clothes      3
      T-Shirt  Clothes      4
"""

使用 StringIO 将样本数据集加载为字符串:

from io import StringIO
import itertools
import pandas as pd

df = pd.read_table(StringIO(datastr),sep='\s+')
def is_not_valid(t):
    """ t is not a valid tuple if sum > 15 or min/max exceeded """
    t_sum = 0
    if t_sum > 15:
        return True
    for item in t:
        t_sum += item[2]
        if t_sum > 15:
            return True
        if item[1] == 'Fruits' and not 1 <= item[2] <= 3:
            return True
        if item[1] == 'Names' and not 2 <= item[2] <= 3:
            return True
        if item[1] == 'Place' and not 1 <= item[2] <= 2:
            return True
        if item[1] == 'Clothes' and not 2 <= item[2] <= 4:
            return True
    return false

all = itertools.combinations(df.values,6)

for t in itertools.filterfalse(is_not_valid,all):
    print('(',end='')
    for j,n in enumerate(t):
        print(n[0],end='')
        if j < 6-1:
            print(',',end='')
    print(')')

输出的开始和结束:

(apple,banana,Krish,Paras,Courtyard,Pant)
(apple,Socks)
# ...
(banana,Pant,Socks,Jacket)
(banana,Jacket,T-Shirt)

大佬总结

以上是大佬教程为你收集整理的列出满足几个条件的组合全部内容,希望文章能够帮你解决列出满足几个条件的组合所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。