Python 3.x 在字典中列出唯一值和位置的所有排列/组合,无需重复,无需使用itertools

Python 3.x 在字典中列出唯一值和位置的所有排列/组合,无需重复,无需使用itertools,python-3.x,dictionary,combinations,permutation,itertools,Python 3.x,Dictionary,Combinations,Permutation,Itertools,新的堆栈溢出,Python,和一般的编码 ===== 我读过并尝试过: 我相信这些都不满足以下条件 当“名称”列表变大时,我的代码要么会遇到递归限制,要么会花费很长时间。再说一次,我是新来的 ===== 排列/组合规则: 给定列表A B C D;(最终名单将更长) A不能有A等 如果A:x,则A不能再次用作A:x 如果x:B,则B不能再次用作x:B 如果列表中的每个字母在每个位置仅使用一次,则只需要len(列表)可能的排列/组合 排列/组合的后续世代不能包含任何先前的排列/组合 不能使用

新的堆栈溢出,Python,和一般的编码

=====

我读过并尝试过:

我相信这些都不满足以下条件

当“名称”列表变大时,我的代码要么会遇到递归限制,要么会花费很长时间。再说一次,我是新来的

=====

排列/组合规则:

给定列表A B C D;(最终名单将更长) A不能有A等

如果A:x,则A不能再次用作A:x

如果x:B,则B不能再次用作x:B

如果列表中的每个字母在每个位置仅使用一次,则只需要len(列表)可能的排列/组合

排列/组合的后续世代不能包含任何先前的排列/组合

不能使用itertools

=====

问题:没有itertools,有没有更好的方法来编写代码

附加问题:有没有办法“告诉”脚本将达到递归限制并安全退出

from random import seed, shuffle
import os
import sys
from datetime import datetime
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
new_seed_num = datetime.now().strftime("%f")
seed(int(new_seed_num))
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
dict_list = []
blocked_tuple = ("A", "B")
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Convert all dictionaries into a list of unique tuples
# This would use a list built from file IO and an earlier list
# Lists not in example
def pairs_exractor(dict_list, blocked_tuple):
    tuple_pairs = []
    if len(blocked_tuple) > 0:
        for blocked_t in blocked_tuple:
            tuple_pairs.append(blocked_t)

    for dct in dict_list:
        temp_pairs_view = dct.items()
        temp_pairs = list(temp_pairs_view)
        for tup_pair in temp_pairs:
            if tup_pair not in tuple_pairs:
                tuple_pairs.append(tup_pair)
            else:
                pass

    return tuple_pairs
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Generate dict of unique pairs that does not match any pairs in any of the lists
def pairs_comparator():
    abcd_list = ["A","B","C","D"]
    shuffle(abcd_list)
    new_pairs_dict = {}
    pop_list_1 = abcd_list[:]
    pop_list_2 = abcd_list[:]
    p_list = pairs_exractor(dict_list, blocked_tuple)
    print(f"Blocked tuple: {p_list}\n")
    # p_list empty in this example except for blocked tuple
    # p_list would be the list of all tuples previously used, from file IO
    for num in range(0,len(abcd_list)):
        shuffle(pop_list_1)
        shuffle(pop_list_2)
        pop_1_name = pop_list_1.pop()
        pop_2_name = pop_list_2.pop()
        popped_tup = (pop_1_name, pop_2_name)
        if pop_2_name != pop_1_name and popped_tup not in p_list:
            new_pairs_dict[pop_1_name] = pop_2_name
        else:
            return pairs_comparator()

    return new_pairs_dict
# User can assign a "name" in list to be blocked in an iteration
# User can assign a "name-pair" to be blocked in an iteration
# Script runs and saves most recent dict to file
# Script then reads file and builds list of dicts from which "name-pairs" cannot match
# In the above example the "names" are "A", "B", "C", "D"
# File IO and list building works and assistance for them is not needed

print(pairs_comparator())
谢谢,, 标记