我想在Python中实现HashMap。我想请用户输入。根据他的输入,我正在从HashMap中检索一些信息。如果用户输入HashMap的键,我想检索相应的值。
如何在Python中实现此功能?
HashMap<String,String> streetno=new HashMap<String,String>(); streetno.put("1", "Sachin Tendulkar"); streetno.put("2", "Dravid"); streetno.put("3","Sehwag"); streetno.put("4","Laxman"); streetno.put("5","Kohli")
Python字典是一种内置的类型,支持键值对。
streetno = {"1": "Sachin Tendulkar", "2": "Dravid", "3": "Sehwag", "4": "Laxman", "5": "Kohli"}
以及使用dict关键字:
streetno = dict({"1": "Sachin Tendulkar", "2": "Dravid"})
要么:
streetno = {} streetno["1"] = "Sachin Tendulkar"