def industries_using_cargo(self, cargo): # segmented by economy result = {} for economy in self.get_economies_sorted_by_name(): accepted_by = self.industry_find_industries_active_in_economy_for_cargo(cargo, economy, 'accept_cargo_types') produced_by = self.industry_find_industries_active_in_economy_for_cargo(cargo, economy, 'prod_cargo_types') if len(list(accepted_by) + list(produced_by)) > 0: result[economy] = {'accepted_by':accepted_by, 'produced_by':produced_by} return result def cargo_unique_industry_combinations(self, cargo): result = {} for economy in self.get_economies_sorted_by_name(): economy_industries = [] accepted_by = self.industry_find_industries_active_in_economy_for_cargo(cargo, economy, 'accept_cargo_types') produced_by = self.industry_find_industries_active_in_economy_for_cargo(cargo, economy, 'prod_cargo_types') for industry in accepted_by: economy_industries.append(industry) for industry in produced_by: economy_industries.append(industry) if len(economy_industries) > 0: industry_key = tuple(sorted(economy_industries)) result.setdefault(industry_key, {'accepted_by': accepted_by, 'produced_by': produced_by}) result[industry_key].setdefault('economies',[]).append(economy) # convenient to have items sorted result[industry_key]['economies'] = sorted(result[industry_key]['economies'], key=lambda economy: self.get_economy_name(economy)) # return a list, sorted by economies (only need first economy entry in each list of economies) return sorted(result.values(), key = lambda combo: self.get_economy_name(combo['economies'][0]))