Multiple keys per value

后端 未结 7 1369
抹茶落季
抹茶落季 2020-12-01 11:58

Is it possible to assign multiple keys per value in a Python dictionary. One possible solution is to assign value to each key:

dict = {\'k1\':\'v1\', \'k2\':         


        
相关标签:
7条回答
  • 2020-12-01 12:41

    I was able to achieve similar functionality using pandas MultiIndex, although in my case the values are scalars:

    >>> import numpy
    >>> import pandas
    >>> keys = [numpy.array(['a', 'b', 'c']), numpy.array([1, 2, 3])]
    >>> df = pandas.DataFrame(['val1', 'val2', 'val3'], index=keys)
    >>> df.index.names = ['str', 'int']
    >>> df.xs('b', axis=0, level='str')
            0
    int      
    2    val2
    
    >>> df.xs(3, axis=0, level='int')
            0
    str      
    c    val3
    
    0 讨论(0)
提交回复
热议问题