0

I need some help in extracting common (shared) values across a python dictionary when I give it a string of text.

Suppose for example that I have a string of text such as:

mylist = ["shirt","pants","shoes","tie","jacket"]

And I also have a list of brands that I have made into dictionary using the dict() function:

shirt   Zara
shirt   GAP
shirt   Old Navy
shirt   Banana_Republic
shoes   Banana_Republic
shoes   Zenga
shoes   Zara
shoes   Nike
shoes   Adidas
tie     Hermes
tie     Ferragamo
tie     Alfani
jacket  Alfani
jacket  Under_Armour
jacket  Nike
jacket  Polo
jacket  The_North_Face

I need my function to return a list of common brands (any two commonalities) in which a given brand does at least two of the categories in my original list. So for: mylist = ['shirt','shoes','tie','jacket']

someFunction(mylist)

returns:

[‘Zara’,’Banana_Republic’,’Alfani’,’Nike’]

At first I tried writing a forloop which essentially does:

brandDictionary = dict(brands)

def mappings(list, dictionary):
for category in list:
    return dictionary[category]

But this returns just one value and I need the string of values

So:

mappings(mylist, brandDictionary)

Gives me:

['Banana_Republic']

And NOT the full list I'm looking for:

[‘Zara’,’Banana_Republic’,’Alfani’,’Nike’]

NOTE ALSO that in $mylist I have "pants" which is NOT stored in my dictionary. This function should just return what values I have matched in my dictionary and not return an error.

atellez
  • 95
  • 1
  • 5

3 Answers3

0

You cannot directly turn that list into a dictionary, as those before have noted.

However, you could build a dictionary whose values are lists or sets, such as

{
"tie": ["Hermes", "Ferragamo", "Alfani"]
...
}

Better yet, build the dictionary with key and value reversed, such as:

{
"Alfamni": ["tie", "jacket"]
...
}

If you are comfortable with set objects, you can make your later processing very short: intersect the dictionary value and the input search target (the mylist you give at the top), and see whether the resulting set has a length of at least 2.

For "pants", just add a check

if item in mydict.keys()
Prune
  • 76,765
  • 14
  • 60
  • 81
0

As I've said in the comments python dictionaries do not support duplicated keys.

With that in mind:

Probably there's a more direct and pythonic way to do this. If I understood your question correctly, this is what you wanted:

script

mylist = ["shirt","pants","shoes","tie","jacket"]

str_data ="""shirt Zara
shirt GAP
shirt Old Navy
shirt Banana_Republic
shoes Banana_Republic
shoes Zenga
shoes Zara
shoes Nike
shoes Adidas
tie Hermes
tie Ferragamo
tie Alfani
jacket Alfani
jacket Under_Armour
jacket Nike
jacket Polo
jacket The_North_Face"""

#dict list
data_dicts = [{line.split()[1]: line.split()[0]} for line in str_data.split("\n")]

clothes_matches = {}

#turn into a single dictionary
for data_dict in data_dicts:
    for key, value in data_dict.items():
        try:
            clothes_matches[key].append(value)
        except KeyError:
            clothes_matches[key] = [value]

#count the number of o occurrences for each brand
result = []
for key,value in clothes_matches.items():
    if len(value) >= 2:
        result.append(key)

print(result)

output

['Zara', 'Nike', 'Alfani', 'Banana_Republic']

Hope this helps! :)

Community
  • 1
  • 1
jlnabais
  • 829
  • 1
  • 6
  • 18
-1

This can be achieved collections.Counter. Make sure your dictionary is structured in such a way that you will not run into a duplicate key error.

from collections import Counter

clothing_map = {
    'Zara': ['shoes', 'shirt'],
    'GAP': ['shirt'],
    'Old_Navy': ['shirt'],
    'Banana_Republic': ['shoes', 'shirt'],
    'Zenga': ['shoes'],
    'Nike': ['jacket', 'shoes'],
    'Adidas': ['shoes'],
    'Hermes': ['tie'],
    'Ferragamo': ['tie'],
    'Alfani': ['jacket', 'tie'],
    'Under_Armour': ['jacket'],
    'Polo': ['jacket'],
    'The_North_Face': ['jacket']
}

def get_brands(items = []):
    l = [key for key, val in clothing_map.items() for x in items if x in val]
    return [key for key, val in Counter(l).items() if val > 1]

mylist = ['shirt', 'pants', 'shoes', 'tie', 'jacket']
print(get_brands(mylist))
#['Alfani', 'Nike', 'Zara', 'Banana_Republic']
Cody Bouche
  • 945
  • 5
  • 10