This question already has answers here:
Python creating a dictionary of lists
(7 answers)
Closed 6 years ago.
I have thousands of products with ingredients of each for example:
ProductID | Ingredients
00001 | itemA, itemB, itemC, itemD
00002 | itemF, itemD, itemG, itemA, itemI
00003 | itemH, itemI, itemD, itemF, itemT,itemB, itemC
........ and so on.
I want to make a unique list of ingredients and make a map that what ingredients are in which product. So For example I want the resulting output in the following way:
{itemA: [00001,00011, 00005,00007]}
{itemB: [00003, 00002, 000056]}
{itemC: [00009, 00087, 00044, 00647, 00031, 00025]}
So the list size will be different for each item. Can somebody help me out in solving this problem? Thanks
Assuming its a text file, it could be something like this:
from collections import defaultdict
product_ingredients_mapping = defaultdict(list)
file_data = open('products.txt')
for row in file_data.readlines():
data = row.split('|')
ingredients = data[1].split(',')
product_id = data[0].strip()
for ingredient in ingredients:
product_ingredients_mapping[ingredient.strip()].append(product_id)
Related
This question already has answers here:
Add params to given URL in Python
(15 answers)
Closed 2 years ago.
I am creating a searching system in a personal website. I have a database with a list of products, and for each product i would like to search it on the website by the requests module.
The main link is: "https://website.com/search/album?uid=1&q="
Example:
I am looking for Iphone 12 256 GB.
The link should be: https://website.com/search/album?uid=1&q=Iphone+12+256+GB.
I've started just defing the main variable:
main_link = "https://website.com/search/album?uid=1&q="
product_name = "Iphone 12 256 GB"
product_name_to_search = product_name.split()
for product_word in product_name_to_search:
main_link + str(product_word)
Frankly, i dont know how to go on.
product_name = product_name.replace(" ","+")
main_link = f"https://website.com/search/album?uid=1&q={product_name}"
Try this:
main_link += '+'.join(product_name.split())
>>> print(main_link)
https://website.com/search/album?uid=1&q=Iphone+12+256+GB
table structure:
-------------------------------------
| id | keywords |
=====================================
| 1 | animals,orange ball |
|-----------|-------------------------|
| 2 |animals,pet, dog,as usual|
|-----------|-------------------------|
| 3 |'anime, animations,superhero,cartoon'|
|_____________________________________|
views.py
TagList = ImgDetails.objects.values_list('keywords').order_by('keywords')
I'm getting value from database in a queryset:
output:
<QuerySet [('animals,orange ball',), ('animals,pet, dog,as usual',), ('animals',), ('animals',), ('animals, pet,dog',), ('animals, pet,dog',), ('animals, pet,dog',), ('animals,fore
st,tiger,lion',), ('animals,forest,tiger,lion',), ('animation,sasuke,cartoon,anime',), ('anime, animations,superhero,cartoon',), ('anime, animations,superhero,cartoo
n',), ('anime, animations,superhero,cartoon',), ('anime, animations,superhero,cartoon',), ('cat,deer',), ('cat,deer',), ('nature, forest, greenry',), ('nature, fores
t, greenry',), ('nature, forest, greenry',)]>
I want to fetch all the unique values or your can say string from the queryset in a list like this:
['animals','orange ball','anime','animations','superhero','cartoon','dog','tiger','forest','animation']
I can use nested for loop to get it done. But is there any simple way to get string from queryset in a list.
I don't know whether its right way of doing it or not but i'm using it:
def getTags():
TagList = ImgDetails.objects.values_list('keywords', flat=True).order_by('keywords')
tagsuggestions = set([item for sublist in TagList for item in str(sublist).split(',')])
return tagsuggestions
You should get list of tuples by following query:
image_details = ImgDetails.objects.annotate(keyword_list=Func(F('keywords'),Value(','),function='string_to_array'))
now you can get list of keywords in keyword_list field.
I have two lists of different sizes. One has Product Titles and other one has brand names ( may be one word or more).
I need to check if the Product Title has exact brand name(present in brand list) mentioned and extract the same else return empty list.
Am facing issue in extracting matching Brand name because brand may contain multiple words.
For eg. :
Following is the input:
Product_Titles =[['Best abc def hair oil'],['laptop erg eds ram 15 GB'],['oops dfr watch']]
Brand_List = [['abc def'],['dfe sdf sd'],['erg eds']]
#Expected Output :
Brand = [['abc def'],['erg eds'],[]]
Getting an empty list for third Product Title because we were not able to get any matching brand with Brand_List.
P.S. :
Only if the complete Brand Name matches then we should return Brand Name.
I have tried Regex but it's not working because if we have 'str' in brand list and 'string' in Product Titles, it will give 'string' as Brand. But I need exact output.
Thanks a ton for all wonderful answers !
I have combined all the below suggestions and came up with my version of the same.
Solution :
Solution Code
You can use a list comprehension:
Product_Titles =[['abc def hair oil'],['erg eds laptop'],['oops dfr watch']]
Brand_List = [['abc def'],['dfe sdf sd'],['erg eds']]
titles = [[[i] if b.startswith(i) else [] for [i] in Brand_List] for [b] in Product_Titles]
final_titles = [filter(None, i)[0] if any(i) else [] for i in titles]
Output:
[['abc def'], ['erg eds'], []]
There should be no difference in how this task can be solved for python 2 or 3, simply loop over the Product_Titles and save matches to a return value:
Product_Titles =[['Best abc def hair oil'],['laptop erg eds ram 15 GB'],['oops dfr watch']]
Brand_List = [['abc def'],['dfe sdf sd'],['erg eds']]
ret = []
for product in Product_Titles:
for brand in Brand_List:
if brand[0] in product[0]:
ret.append(brand)
break
else:
ret.append([])
print(ret)
>> [['abc def'], ['erg eds'], []]
If any of the idioms are unclear, feel free to ask what they mean. Also, if line space is precious, this solution can be expressed as a list comprehension as well:
ret = [brand[0] for brand in Brand_List if brand[0] in prod[0]] for prod in Product_Titles]
print(ret)
>> [['abc def'], ['erg eds'], []]
There is a difference between these two, in so far that the break does not exist in the comprehension. If multiple brands exist in a single product, they will all be listed in the returned list. Which seems reasonable though, I would actually assume that you'd want this behavior in the loop as well.
Also, note that the google python stylesheet discourages using comprehensions that are longer than one line (80 characters), and to use loops instead.
I have iterator in which it gives me the version naming, user name(s) and size affiliated with it.
I made it into a dictionary while making the user name and size as a tuple as a value in the dict.
gen_dict = {
"item_A_v004": ["jack", "1"],
"item_A_v007": ["jack", "10"],
"item_A_v009": ["jack", "1000"],
"item_A_v008": ["jack", "100"],
"item_B_v001": ["kay", "200"],
"item_B_v003": ["peter", "2200"],
"item_B_v005": ["kay", "20"],
"item_C_v004": ["kay", "3"]
}
This is the output I got:
| item_A_v(xxx) | 4 | jack [1111 (4)] |
| item_B_v(xxx) | 3 | kay [223 (2)], peter [2200 (1)] |
| item_C_v(xxx) | 1 | kay [223 (1)] |
This is the code:
asset_user_dict = defaultdict(set)
asset_count = defaultdict(int)
user_ver_count = defaultdict(lambda: defaultdict(int))
asset_size_dict = {}
for vers_name, artist_alias in gen_dict.iteritems():
strip_version_name = vers_name[:-3]
asset_user_dict[strip_version_name].add(artist_alias[0])
asset_count[strip_version_name] += 1
user_ver_count[artist_alias[0]][strip_version_name] += 1
# This is my problem here...
# Seems to be summing up all the data found under 1 user
asset_size_dict.setdefault(artist_alias[0], []).append(artist_alias[1])
for version_name, version_count in sorted(asset_count.iteritems()):
users = ', '.join('{0} [{1} ({2}]'.format(alias, convert_size_query(sum(int(i) for i in asset_size_dict.get(alias))), user_ver_count[alias][version_name]) for alias in asset_user_dict[version_name])
#print users
print "| {0:<100} | {1:>12} | {2:>50} |".format(version_name+"(xxx)",
version_count,
users
)
It seems that if I have same user names (see kay) between versions, the overall sum of the size is being outputted instead. So in the above example, I am expecting Kay's Item_B to be 220 and item_C to be 3 but I got 223 for both.
I tried several ways of using more dictionaries but I have either made myself more confused or I simply could not find a 'link' as I wanted to achieve the following:
Collate similar version namings into 1
Prior to #1, any user names affiliated with the similar versions will be outputted besides it
Prior to #2, it will also derive the overall size that each user occupies as well as the number of versions the user have
So far I seem to be able to do for all points but I am definitely missing out a few steps to achieve this 'same user' issue and sorting the third column in descending order by using the size.
What other better ways are there for me to tackle on this problem?
This question already has answers here:
How do I merge two dictionaries in a single expression in Python?
(43 answers)
Closed 7 years ago.
The question is
This is what I have so far:
dict(nafta_capitals) = canadian_capitals, mexican_capitals, us_capitals
Given three dictionaries, associated with the variables , canadian_capitals, mexican_capitals, and us_capitals, that map provinces or states to their respective capitals, create a new dictionary that combines these three dictionaries, and associate it with a variable , nafta_capitals.
You may need to use defaultdict-
Here nafta is used as key to the three ( canadian_capitals, mexican_capitals, us_capitals) as below-
>>>dic = defaultdict(list)
>>>lst = ['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1', 'nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']
>>>grouped_lst = [lst[i:i+4] for i in range(0,len(lst),4)]
>>>[['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1'], ['nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']]
>>>for i in grouped_lst:dic[i[0]]=i[1:]
>>>dic.items()
>>>[('nafta1', ['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']), ('nafta2', ['canadian_capitals2', 'mexican_capitals2', 'us_capitals2'])]
>>>for i in dic.keys():print dic[i]
>>>['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']
['canadian_capitals2', 'mexican_capitals2', 'us_capitals2']