How can I get dictionary key as variable directly in Python (not by searching from value)?

前端 未结 14 1729
面向向阳花
面向向阳花 2020-12-04 06:19

Sorry for this basic question but my searches on this are not turning up anything other than how to get a dictionary\'s key based on its value which I would prefer not to us

相关标签:
14条回答
  • 2020-12-04 07:01

    What I sometimes do is I create another dictionary just to be able whatever I feel I need to access as string. Then I iterate over multiple dictionaries matching keys to build e.g. a table with first column as description.

    dict_names = {'key1': 'Text 1', 'key2': 'Text 2'}
    dict_values = {'key1': 0, 'key2': 1} 
    
    for key, value in dict_names.items():
        print('{0} {1}'.format(dict_names[key], dict_values[key])
    

    You can easily do for a huge amount of dictionaries to match data (I like the fact that with dictionary you can always refer to something well known as the key name)

    yes I use dictionaries to store results of functions so I don't need to run these functions everytime I call them just only once and then access the results anytime.

    EDIT: in my example the key name does not really matter (I personally like using the same key names as it is easier to go pick a single value from any of my matching dictionaries), just make sure the number of keys in each dictionary is the same

    0 讨论(0)
  • 2020-12-04 07:03

    The reason for this is that I am printing these out to a document and I want to use the key name and the value in doing this

    Based on the above requirement this is what I would suggest:

    keys = mydictionary.keys()
    keys.sort()
    
    for each in keys:
        print "%s: %s" % (each, mydictionary.get(each))
    
    0 讨论(0)
  • 2020-12-04 07:05

    This code below is to create map to manager players point. The goal is to concatenate the word "player" with a sequential number.

    players_numbers = int(input('How many girls will play? ')) #First - input receive a input about how many people will play
    
    players = {}  
    counter = 1
    
    for _ in range(players_numbers): #sum one, for the loop reach the correct number
        player_dict = {f'player{counter}': 0} #concatenate the word player with the player number. the initial point is 0.
        players.update(player_dict) #update the dictionary with every player
        counter = counter + 1
        print(players)
    

    Output >>> {'player1': 0, 'player2': 0, 'player3': 0}...

    0 讨论(0)
  • 2020-12-04 07:06

    if you just need to get a key-value from a simple dictionary like e.g:

    os_type = {'ubuntu': '20.04'}
    

    use popitem() method:

    os, version = os_type.popitem()
    print(os) # 'ubuntu'
    print(version) # '20.04'
    
    0 讨论(0)
  • 2020-12-04 07:11

    You can do this by casting the dict keys and values to list. It can also be be done for items.

    Example:

    f = {'one': 'police', 'two': 'oranges', 'three': 'car'}
    list(f.keys())[0] = 'one'
    list(f.keys())[1] = 'two'
    
    list(f.values())[0] = 'police'
    list(f.values())[1] = 'oranges'
    
    0 讨论(0)
  • 2020-12-04 07:13

    You could simply use * which unpacks the dictionary keys. Example:

    d = {'x': 1, 'y': 2}
    t = (*d,)
    print(t) # ('x', 'y')
    
    0 讨论(0)
提交回复
热议问题