93975


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

[code]

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

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

  1. それぞれのgroupの中で値を照合
  2. 違う種類がどれくらいあるか判別（group１の中では、aの1は2つあり、aの2は1つ、bの1は1つなので、3種類存在する）し、それぞれのグループごとにアルファベットの大文字を書き出す。
  3. 結果を立て並びに出力する

以下のように出力したいです。

[code]

    result = 
    4       #何種類あるか示します
    group2
    a  1  E,F     #dict2,dict3,dict1の情報を示します
    a  2  G
    a  3  H
    a  4  I
    
    3       
    group1
    a 　1　 A,D
    a 　2 　B
    b 　1 　C
    
    group3
    a 　1　 J,K
    a 　2 　L
    b 　2 　M,N
    
[/code]

知識不足ながらプログラム案を考えてみましたが、種類が大きい順に示すこととグループごとに大文字アルファベットを示すことができません。ご教示お願い致します。

[code]

    set_list = {k: len(set(zip(dict2[k], dict3[k]))) for k in dict2.keys()}
    result = []
    for r in set_list.keys():
      result.append(set_list[r])
      result.append(list(set(zip(dict2[r], dict3[r]))))
      result.append(dict1[r])
    
    #出力
    [3, [('a', '1'), ('a', '2'), ('b', '1')], ['A', 'B', 'C', 'D'], 4, [('a', '3'), ('a', '1'), ('a', '2'), ('a', '4')], ['E', 'F', 'G', 'H', 'I'], 3, [('a', '1'), ('b', '2'), ('a', '2')], ['J', 'K', 'L', 'M', 'N']]
    
[/code]


CC BY-SA 4.0
2023-02-24T04:13:47.563
93969
2023-02-24T07:36:25.800
2023-02-24T04:46:41.173
3060
57218
question
python
Pythonでの複数ある辞書の比較について


dict2,3値をキーとし、それに対応するdict1値をリストに格納することで、求める結果を得ることができます。

[code]

    dict1 = {"group1":["A","B","C","D"],"group2":["E","F","G","H","I"],"group3":["J","K","L","M","N"]}
    dict2 = {"group1":["a","a","b","a"],"group2":["a","a","a","a","a"],"group3":["a","a","a","b","b"]}
    dict3 = {"group1":["1","2","1","1"],"group2":["1","1","2","3","4"],"group3":["1","1","2","2","2"]}
    
    for key in dict1.keys():
    
        # 2,3の値をキーとし、それに対応する1の値をリストに格納
        sub_d = {}
        for d1, d2, d3 in zip(dict1[key], dict2[key], dict3[key]):
            sub_k = (d2, d3)
            if sub_k not in sub_d:
                sub_d[sub_k] = []
            sub_d[sub_k].append(d1)
    
        # 結果の出力
        print(len(sub_d))
        print(key)
        for k, v in sub_d.items():
            print(k[0], k[1], *v)
    """
    3
    group1
    a 1 A D
    a 2 B
    b 1 C
    4
    group2
    a 1 E F
    a 2 G
    a 3 H
    a 4 I
    3
    group3
    a 1 J K
    a 2 L
    b 2 M N
    """
    
[/code]


CC BY-SA 4.0
2023-02-24T04:30:46.470
93970
2023-02-24T04:30:46.470
25830
93969
answer


groupby 使う方法

[code]

    dict1 = {"group1":["A","B","C","D"],"group2":["E","F","G","H","I"],"group3":["J","K","L","M","N"]}
    dict2 = {"group1":["a","a","b","a"],"group2":["a","a","a","a","a"],"group3":["a","a","a","b","b"]}
    dict3 = {"group1":["1","2","1","1"],"group2":["1","1","2","3","4"],"group3":["1","1","2","2","2"]}
    
    from itertools import groupby
    g_key = lambda v: v[1:]
    for k in dict1:
        res = [(k2, ','.join(u for u,*_ in g))
                for k2,g in groupby(sorted(zip(dict1[k], dict2[k], dict3[k]), key=g_key), key=g_key)]
    
        print(len(res))
        print(k)
        for item in res:
            print(f'{item[0][0]}\t{item[0][1]}\t{item[1]}')
    
[/code]


CC BY-SA 4.0
2023-02-24T06:08:43.543
93973
2023-02-24T06:08:43.543
43025
93969
answer


itertools の groupby を使う方法。

[code]

    from itertools import groupby
    
    dict1 = {"group1":["A","B","C","D"],"group2":["E","F","G","H","I"],"group3":["J","K","L","M","N"]}
    dict2 = {"group1":["a","a","b","a"],"group2":["a","a","a","a","a"],"group3":["a","a","a","b","b"]}
    dict3 = {"group1":["1","2","1","1"],"group2":["1","1","2","3","4"],"group3":["1","1","2","2","2"]}
    
    keyfunc = lambda x: x[:2]
    result = [(g, {k: [i[-1] for i in v]
                  for k, v in groupby(sorted(zip(dict2[g], dict3[g], dict1[g]), key=keyfunc), key=keyfunc)})
              for g in dict1.keys()]
    result.sort(key=lambda i: len(i[1]), reverse=True)
    
    for g in result:
        print(len(g[1]))
        print(g[0])
        for i, j in g[1].items():
            print(*i, ','.join(j), sep='\t')
        print()
    
    # 4
    # group2
    # a 1   E,F
    # a 2   G
    # a 3   H
    # a 4   I
    # 
    # 3
    # group1
    # a 1   A,D
    # a 2   B
    # b 1   C
    # 
    # 3
    # group3
    # a 1   J,K
    # a 2   L
    # b 2   M,N
    
[/code]


CC BY-SA 4.0
2023-02-24T06:43:14.083
93975
2023-02-24T07:36:25.800
2023-02-24T07:36:25.800
47127
47127
93969
answer