Hash Map in Python

后端 未结 9 1069
礼貌的吻别
礼貌的吻别 2020-11-27 10:59

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 k

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 11:22

    In python you would use a dictionary.

    It is a very important type in python and often used.

    You can create one easily by

    name = {}
    

    Dictionaries have many methods:

    # add entries:
    >>> name['first'] = 'John'
    >>> name['second'] = 'Doe'
    >>> name
    {'first': 'John', 'second': 'Doe'}
    
    # you can store all objects and datatypes as value in a dictionary
    # as key you can use all objects and datatypes that are hashable
    >>> name['list'] = ['list', 'inside', 'dict']
    >>> name[1] = 1
    >>> name
    {'first': 'John', 'second': 'Doe', 1: 1, 'list': ['list', 'inside', 'dict']}
    

    You can not influence the order of a dict.

提交回复
热议问题