94289


値の左から順に同じグループになります。  
例えば、group1では、Aのaの1が一つのグループで、また別に、Aのaの2、Bのaの2、Cのbの1、Dのaの1、Dのaの3、Dのbの3となり、7つのグループが存在することになります。

[code]

    dict1 = {"group1":["A","A","B","D","C","D","D"],"group2":["E","F","G","H","I"],"group3":["J","N","K","L","K","M","N"]}
    dict2 = {"group1":["a","a","a","b","b","a","a"],"group2":["a","a","a","a","a"],"group3":["a","a","a","a","a","b","b"]}
    dict3 = {"group1":["1","2","2","3","1","1","3"],"group2":["1","1","2","3","4"],"group3":["1","1","1","2","2","2","2"]}
    
[/code]

以下がdict1,dict2,dict3という辞書に対して行いたい処理です。

それぞれのgroupの中で値を照合  
アルファベットの小文字を基に数値を比べ、同じ大文字がどれくらいあるか判別（group１の中では、aの1はA,Dがあり、aの2ではAがあるので、同じ大文字のAを抜き出す。  
また、aの1はA,Dがあり、aの3ではDがあるので、同じ大文字のDを抜き出す。しかし、aの1はA,Dがあり、bの3ではDがあるが、aとbが違うので比較しない。）し、  
それぞれのグループごとにアルファベットの大文字を書き出す。  
結果を立て並びに出力する  
以下のように出力したいです。

[code]

    result =
    
    group1
    a  1 , a  2   A  #dict2,dict3,dict1の情報を示します
    a  1 , a  3   D
    a  2 , a  3
    b  1 , b  3 
    
    group2
    a  1 , a  2   
    a  1 , a  3   
    a  1 , a  4   
    a  2 , a  3   
    a  2 , a  4   
    a  3 , a  4   
    
    group3
    a  1 , a  2   K
    
[/code]

知識不足ながらプログラム案を考えてみましたが、組み合わせをして、共通のものを取り出すことができません。ご教示お願い致します。

[code]

    from itertools import groupby
    
    
    keyfunc = lambda x: x[:2]
    result_ctype = [(g, {k: [i[-1] for i in v]
               for k, v in groupby(sorted(zip(dict3[g], dict2[g], dict1[g]), key=keyfunc), key=keyfunc)})
          for g in dict1.keys()]
    
    #出力
    [('group1', {('a', '1'): ['A', 'D'], ('a', '2'): ['A', 'B'], ('a', '3'): ['D'], ('b', '1'): ['C'], ('b', '3'): ['D']}), ('group2', {('a', '1'): ['E', 'F'], ('a', '2'): ['G'], ('a', '3'): ['H'], ('a', '4'): ['I']}), ('group3', {('a', '1'): ['J', 'N', 'K'], ('a', '2'): ['L', 'K'], ('b', '2'): ['M', 'N']})]
    
[/code]


CC BY-SA 4.0
2023-03-23T17:56:35.877
94288
2023-03-23T19:58:23.483
57218
question
python
Python　辞書での組み合わせと重複について

[code]

    from itertools import groupby, combinations
    
    dict1 = {"group1":["A","A","B","D","C","D","D"],"group2":["E","F","G","H","I"],"group3":["J","N","K","L","K","M","N"]}
    dict2 = {"group1":["a","a","a","b","b","a","a"],"group2":["a","a","a","a","a"],"group3":["a","a","a","a","a","b","b"]}
    dict3 = {"group1":["1","2","2","3","1","1","3"],"group2":["1","1","2","3","4"],"group3":["1","1","1","2","2","2","2"]}
    
    kf = lambda x: x[:2]
    for g in dict1.keys():
        d = {k: [i[-1] for i in v]
             for k, v in groupby(sorted(zip(dict2[g], dict3[g], dict1[g]), key=kf), key=kf)}
        print(g)
        for _, i in groupby(d.keys(), key=lambda k: k[0]):
            for a, b in combinations(i, 2):
                print(*a, ',', *b, *(set(d[a]) & set(d[b])))
    
    # group1
    # a 1 , a 2 A
    # a 1 , a 3 D
    # a 2 , a 3
    # b 1 , b 3
    # group2
    # a 1 , a 2
    # a 1 , a 3
    # a 1 , a 4
    # a 2 , a 3
    # a 2 , a 4
    # a 3 , a 4
    # group3
    # a 1 , a 2 K
    
[/code]


CC BY-SA 4.0
2023-03-23T19:47:07.283
94289
2023-03-23T19:58:23.483
2023-03-23T19:58:23.483
47127
47127
94288
answer