Home>

I want to rewrite the Java code below to Python. With this code, there is a hash map in the hash map, so I don't know how to write it.

HashMap<String, HashMap<String, String>>Map = new HashMap<String, HashMap<String, String>>();
Map.put (array [0], new HashMap<String, String>());
Map.get (array [0]). Put (array [i], array [i + 1]);


In Python, I would like to use a dictionary with a function similar to a hash map, but since the hash map is duplicated, should I write a two-dimensional dictionary?
I checked the 2D dictionary and wrote the code. Is it the same as the Java code above?

Map = {}
Map [List [0]] = {}
Map [List [0]] [List [i]] = List [i + 1]
  • Answer # 1

    I think that's fine.

    list = [
        "aaa",
        "bbb",
        "ccc"
    ]
    Map = {}
    Map [List [0]] = {}
    i = 1
    Map [List [0]] [List [i]] = List [i + 1]
    print (Map)
    # {'aaa': {'bbb': 'ccc'}}