Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从组合列表中选择对的最佳策略_Python_List_Combinations - Fatal编程技术网

Python 从组合列表中选择对的最佳策略

Python 从组合列表中选择对的最佳策略,python,list,combinations,Python,List,Combinations,问题:有人能帮我弄清楚如何计算每个循环中最多有三对的循环吗?见最后一个例子 这就是我想做的: ->每个周期配对两个用户,以便 -在给定的周期内,每个用户仅与另一个用户配对一次 -在所有周期中,每个用户仅与每个其他用户配对一次 现实世界: 你每周从一张名单中结识一位新人=周期。 你再也见不到同一个人了。 每个用户每周都与其他用户匹配 这是我的问题: 我能够创建用户组合,并选择从未见过面的用户对。然而,有时我只能在一个循环中匹配两对,而不是三对。因此 我正在寻找一种从组合列表中创建最佳选择的方法 1

问题:有人能帮我弄清楚如何计算每个循环中最多有三对的循环吗?见最后一个例子

这就是我想做的: ->每个周期配对两个用户,以便 -在给定的周期内,每个用户仅与另一个用户配对一次 -在所有周期中,每个用户仅与每个其他用户配对一次

现实世界: 你每周从一张名单中结识一位新人=周期。 你再也见不到同一个人了。 每个用户每周都与其他用户匹配

这是我的问题: 我能够创建用户组合,并选择从未见过面的用户对。然而,有时我只能在一个循环中匹配两对,而不是三对。因此 我正在寻找一种从组合列表中创建最佳选择的方法

1我从6个用户开始:

users = ["A","B","C","D","E","F"]
2从该列表中,我创建了可能的组合:

 x = itertools.combinations(users,2)
    for i in x:
        candidates.append(i)
这给了我:

 .   A,B  A,C  A,D A,E A,F
 .    .   B,C  B,D B,E B,F
 .    .    .   C,D C,E C,F
 .    .    .    .  D,E D,F
 .    .    .    .   .  E,F

3现在,我想从这个列表中选择pairs,这样一个用户a到F只出现一次&在这个循环中,所有用户都与某人配对

例如:

cycle1 = ('A','B'),('C','D') ('E','F')
cycle 1: AF BC DE
cycle 2: AB CD EF
cycle 3: AC BE DF
cycle 4: AE BD CF
cycle 5: AD BF CE
下一个周期,我想找到另一组三对

我计算出,对于6个用户,应该有5个周期,每个周期有3对:

例如:

cycle1 = ('A','B'),('C','D') ('E','F')
cycle 1: AF BC DE
cycle 2: AB CD EF
cycle 3: AC BE DF
cycle 4: AE BD CF
cycle 5: AD BF CE

有人能帮我找出如何计算每个周期最多有三对的周期吗?见最后一个示例?

好的,这是伪代码,但应该可以做到

其中: 移除x,xs从xs移除x RemoveOccurncesx,xs删除至少包含该对`x中一个元素的每对xs


编辑:停止算法的条件可能需要进一步考虑…

以下是基于itertools的解决方案:

import itertools

def hasNoRepeats(matching):
    flattenedList = list(itertools.chain.from_iterable(matching))
    flattenedSet = set(flattenedList)
    return len(flattenedSet) == len(flattenedList)

def getMatchings(users, groupSize=2):
#   Get all possible pairings of users
    pairings = list(itertools.combinations(users, groupSize))
#   Get all possible groups of pairings of the correct size, then filter to eliminate groups of pairings where a user appears more than once
    possibleMatchings = filter(hasNoRepeats, itertools.combinations(pairings, len(users)/groupSize))
#   Select a series of the possible matchings, making sure no users are paired twice, to create a series of matching cycles.
    cycles = [possibleMatchings.pop(0)]
    for matching in possibleMatchings:
        # pairingsToDate represents a flattened list of all pairs made in cycles so far
        pairingsToDate = list(itertools.chain.from_iterable(cycles))
        # The following checks to make sure there are no pairs in matching (the group of pairs being considered for this cycle) that have occurred in previous cycles (pairingsToDate)
        if not any([pair in pairingsToDate for pair in matching]):
            # Ok, 'matching' contains only pairs that have never occurred so far, so we'll add 'matching' as the next cycle
            cycles.append(matching)
    return cycles

# Demo:

users = ["A","B","C","D","E","F"]

matchings = getMatchings(users, groupSize=2)

for matching in matchings:
    print matching
输出:

(('A', 'B'), ('C', 'D'), ('E', 'F'))
(('A', 'C'), ('B', 'E'), ('D', 'F'))
(('A', 'D'), ('B', 'F'), ('C', 'E'))
(('A', 'E'), ('B', 'D'), ('C', 'F'))
(('A', 'F'), ('B', 'C'), ('D', 'E'))

Python 2.7。这有点粗暴,但它完成了任务。

就像唐在评论中提到的那样,你的问题实际上相当于创造一个新的环境。这是Wikipedia页面上提到的算法的Python版本,另请参见和

def scheduleusers: 首先复制或转换为长度为`n的列表` 用户=列表用户 n=用户 为人数不均的参与者添加虚拟对象 如果n%2: 用户。附加“用户” n+=1 周期=[] 对于rangen-1中的uu: 折叠,`/`用于整数除法 c=zipusers[:n//2],reversedusers[n//2:] 附录C 旋转,将用户0固定到位 users.insert1,users.pop 返回周期 附表['A','B','C','D','E','F'] 对于您的示例,它生成以下内容:

[A',F',B',E',C',D'], [A',E',F',D',B',C'], [A',D',E',C',F',B'], [A',C',D',B',E',F'], ['A','B','C','F','D','E']]
非常有趣的解决方案。非常感谢。如果没有[pairingsToDate for Pairin matching]的话,我很难理解:您是否愿意在代码段中添加注释?例如,paringsToDate的设置和更改在哪里?@user2130988-添加了一些注释-这有帮助吗?太好了。这给了我一些新的功能来研究和解决我的问题!在我看来,你想要的是一个循环赛安排算法: