I want to implement a HashMap in Python. I want to ask a user for an input. depending on his input I am retrieving some information from the HashMap. If the user enters a key of the HashMap, I would like to retrieve the corresponding value.
How do I implement this functionality in Python?
HashMap streetno=new HashMap();
streetno.put("1", "Sachin Tendulkar");
streetno.put("2", "Dravid");
streetno.put("3","Sehwag");
streetno.put("4","Laxman");
streetno.put("5","Kohli")
解决方案
Python dictionary is a built-in type that supports key-value pairs.
streetno = {"1":"Sachine Tendulkar", "2":"Dravid", "3":"Sehwag", "4":"Laxman","5":"Kohli"}
as well as using the dict keyword:
streetno = dict({"1":"Sachine Tendulkar", "2":"Dravid"})
or:
streetno = {}
streetno["1"] = "Sachine Tendulkar"