The following problem is more an algorithm one than a code challenge.
Imagine I have a data structure as follows:
cities = {'price' : ['malaga','berlin'],
'food' : ['milano','barcelona'],
'shopping': ['milano','barcelona'],
'weather' : ['barcelona','paris','lisabon','milano'],
'museums' : ['malaga','berlin','lisabon'],
'cafes' : ['paris','roma','lisabon'],
'kids' : ['milano','barcelona','paris','roma']}
There are a bunch of characteristics that can be found in different cities. What is the minimal amount of cities that covers all those characteristics. I.e. the minimal amount of cities I have to visit in order to get all the benefits.
So far I started using Counter
totals=[]
for key in cities.keys():
totals.append(cities[key])
totals_together = [city for cities in totals for city in cities]
totals_together
myCounter = Counter(totals_together)
print(myCounter.most_common())
RESULT so far:
[('milano', 4), ('barcelona', 4), ('paris', 3), ('lisabon', 3), ('malaga', 2), ('berlin', 2), ('roma', 2)]
myCounter gives me an idea of the best cities, but by far not yet the optimal combination of cities. from here I could get the first city, get the characteristics, and go on adding characteristics till all are there. Very tedious.
There should be a better way.
I am even thinking about pandas, but can not see what pandas would bring for this problem. This looks to me a pretty common problem.
NOTE: I am not even looking for the code as such, just ideas about how to embrace the problem are more than welcome.
NOTE2: Be aware that there might be one or more cities with all the characteristics, but there might be the case (normally) where there is no single city with all the characteristics.
SO THE RESULT I AM LOOKING FOR IS: ['milano','lisabon'] assuming that this combination covers all the characteristics.