I get the data in json:

"chestCycle": { "upcoming": [ "silver", "silver", "gold", "silver", "silver", "silver", "silver", "gold", "silver" ], 

I translate these values:

 self.icons = { "silver": "Серебрянный сундук", "gold": "Золотой сундук", "superMagical": "Супер магический сундук", "magical": "Магический сундук", "legendary": "Легендарный сундук", "epic": "Эпический сундук", "giant": "Гиганский сундук", } 

Trying to withdraw:

 player = await client.get_player(text) soobs = f"След сундуки: {self.icons[player.chestCycle.upcoming]}" print(soobs) 

But it gives an error (

 soobs = f"След сундуки: {self.icons[player.chestCycle.upcoming]}" 

TypeError: unhashable type: 'BoxList'

  • What do you have in player.chestCycle.upcoming? - insolor
  • "chestCycle": {"upcoming": ["silver", "silver", "gold", "silver", "silver", "silver", "silver", "gold", "silver"], "superMagical" : 521, "magical": 67, "legendary": 543, "epic": 451, "giant": 35}, - K-9
  • and that's all you are trying to use as a key in the dictionary? - insolor
  • I'm still in this noob (Please indicate the error - K-9
  • Well, as if in self.icons you have the keys of the string silver, gold, etc., and you transfer the dictionary as a key there. - insolor

1 answer 1

A Python dictionary is (externally) a very simple thing. It just has the keys and the values ​​it matches. If we give the key to the "input" of the dictionary, we will get the value corresponding to this key. It is impossible to file more than one key (more precisely, you can, but you will not get anything good - at best, KeyError ). You can’t send an unhashable value to the input as a key (to which the hash() function cannot be applied) - get an error TypeError: unhashable type . The dictionary inside works as a hash table (I highly recommend this article to study), hence the requirement that the standard Python hash function should be applicable to keys.

You are trying to transfer a list of keys to the dictionary, and not just a list, but a list inside some container ( BoxList ). The dictionary does not have artificial intelligence, and he will not understand what you wanted from him, but will simply assume that you have transferred such a key to it. The BoxList passed object of class BoxList not a hashable object, so you get an error TypeError: unhashable type .

To get the list of values ​​from the list of keys from the dictionary, then you need to cycle through the original list and substitute each key in the dictionary.

Example, you have a list of keys and a dictionary:

 upcoming = [ "silver", "silver", "gold", "silver", "silver", "silver", "silver", "gold", "silver" ] icons = { "silver": "Серебрянный сундук", "gold": "Золотой сундук", "superMagical": "Супер магический сундук", "magical": "Магический сундук", "legendary": "Легендарный сундук", "epic": "Эпический сундук", "giant": "Гиганский сундук", } 

Under this list, go through the cycle, substitute each key in the dictionary

 print([icons[key] for key in upcoming]) # Вывод: ['Серебрянный сундук', 'Серебрянный сундук', 'Золотой сундук', 'Серебрянный сундук', 'Серебрянный сундук', 'Серебрянный сундук', 'Серебрянный сундук', 'Золотой сундук', 'Серебрянный сундук'] 

A person needs to show not a list of lines (in square brackets, each line in quotes), but simply an enumeration of the chests. To do this, combine this list of strings into a single string, separated by commas, using the join() method:

 print(', '.join(icons[key] for key in upcoming)) # Вывод: Серебрянный сундук, Серебрянный сундук, Золотой сундук, Серебрянный сундук, Серебрянный сундук, Серебрянный сундук, Серебрянный сундук, Золотой сундук, Серебрянный сундук 

Well, your message will be formed like this:

 soobs = "След сундуки: " + ', '.join(self.icons[key] for key in player.chestCycle.upcoming) 

This will only work if the BoxList object supports iteration over it.

  • Thank you so much, I discovered a lot for myself, good luck to you) - K-9