1

I am basically looking at a construct to allow me to map multiple strings to the same value. So something like

Ford/Chrysler/Nissan ==> Cars
Raspberry/Blackberry/Blueberry ==> Berry
Apple/Mango/Bananas ==> Fruit

So I should arrive at the basetype by giving the specific word. What would be the best data structure for such a combination? Since it is not a good practice to search for keys when you know a value. This means a dict is out of question.

Perhaps a hash function where all the above similar ones all map to the same base type if you know what I mean?

AjB
  • 890
  • 13
  • 34
  • 1
    a banana is a berry :) im not sure i understand your question. you want to be able to get "cars" when you have the word "Ford" or the other way around? – Nullman Feb 22 '17 at 05:34
  • I don't know of any ready built class for this but you could create a dictionary whose values are keys to an array. For example `x = {'Ford':0, 'Nissan':0, 'Apple':1}` and `y = ['Cars', 'Fruit']`. – user2027202827 Feb 22 '17 at 05:45
  • Yes I want to get to 'cars' when i have Ford or Nissan for that matter :). Btw is Banana really a berry? Also user could you elaborate? – AjB Feb 22 '17 at 06:18

1 Answers1

0

Try using enum

Enums have been added to Python 3.4

or Dictionary

d=dict()
d['Car']=('Ford','Chrysler','Nissan')
d['Berry']=('Raspberry','Blackberry','Blueberry')
#if you want to add 
d['Car'] = d['Car']+('Volkswagen',)
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
  • I thought of that already, but although it is doable, it is not the `correct` way to look at and use a dictionary http://stackoverflow.com/questions/2568673/inverse-dictionary-lookup-in-python http://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary May be I'd have to use those methods anyways but if there is a better data structure that can do it, I'm all ears – AjB Feb 22 '17 at 06:20