84004


以下のような辞書を作りたいです.

`要素の値` → `(行,列):要素の値` に変更し, 出力するプログラムを作りたいです.  
どのようにプログラムを組めばできますか？

**目的の辞書:**

[code]

     c={
       (1,1):2,(1,2):3,(1,3):3,(1,4):5,
       (2,1):2,(2,2):3,(2,3):3,(2,4):5,
       (3,1):2,(3,2):3,(3,3):3,(3,4):5,
       (4,1):2,(4,2):3,(4,3):3,(4,4):5,
       (5,1):2,(5,2):3,(5,3):3,(5,4):5,
        }
    
[/code]

**元となる配列:**

[code]

    c=[
     [2.  3.  3.  5.]
     [2.  3.  3.  5.]
     [2.  3.  3.  5.]
     [2.  3.  3.  5.]
     [2.  3.  3.  5.]]
    
[/code]


CC BY-SA 4.0
2021-12-07T08:16:48.967
84003
2021-12-07T11:52:34.267
2021-12-07T11:19:15.883
3060
48372
question
python python3 numpy
python 要素の値→(行,列):要素の値 への変更の仕方


愚直な方法です。

[code]

    a = [
        [2,  3,  3,  5],
        [2,  3,  3,  5],
        [2,  3,  3,  5],
        [2,  3,  3,  5],
        [2,  3,  3,  5],
    ]
    
    c = {}
    
    for i, row in enumerate(a):
        for j, element in enumerate(row):
            c[(i + 1, j + 1)] = element
    
    print(c)
    
[/code]

[code]

    {(1, 1): 2, (1, 2): 3, (1, 3): 3, (1, 4): 5, (2, 1): 2, (2, 2): 3, (2, 3): 3, (2, 4): 5, (3, 1): 2, (3, 2): 3, (3, 3): 3, (3, 4): 5, (4, 1): 2, (4, 2): 3, (4, 3): 3, (4, 4): 5, (5, 1): 2, (5, 2): 3, (5, 3): 3, (5, 4): 5}
    
[/code]


CC BY-SA 4.0
2021-12-07T09:10:58.277
84004
2021-12-07T09:10:58.277
36010
84003
answer


Dict comprehension + `itertools.product`.

[code]

    from itertools import product
    
    c = [
      [2,  3,  3,  5],
      [2,  3,  3,  5],
      [2,  3,  3,  5],
      [2,  3,  3,  5],
      [2,  3,  3,  5],
    ]
    
    c = {(i+1, j+1): c[i][j] for i, j in product(range(len(c)), range(len(c[0])))}
    
    from pprint import pprint
    pprint(c)
    
    #
    {
      (1, 1): 2, (1, 2): 3, (1, 3): 3, (1, 4): 5,
      (2, 1): 2, (2, 2): 3, (2, 3): 3, (2, 4): 5,
      (3, 1): 2, (3, 2): 3, (3, 3): 3, (3, 4): 5,
      (4, 1): 2, (4, 2): 3, (4, 3): 3, (4, 4): 5,
      (5, 1): 2, (5, 2): 3, (5, 3): 3, (5, 4): 5
    }
    
[/code]


CC BY-SA 4.0
2021-12-07T11:52:34.267
84007
2021-12-07T11:52:34.267
47127
84003
answer