Create tuple from key-value - python

I have a list with dictionary inside, with the following keys: id, external_id_client, product_id, as below:
[{'id': 3, 'external_id_client': '7298', 'product_id': 13},
{'id': 3, 'external_id_client': '7298', 'product_id': 8},
{'id': 4, 'external_id_client': '3', 'product_id': 12},
{'id': 4, 'external_id_client': '3', 'product_id': 9},
{'id': 5, 'external_id_client': '4', 'product_id': 12}]
I need that if the ID and external_id are repeated and I have information in the product_id key it creates a tuple in the product_id.
Expected output:
[{'id': 3, 'external_id_client': '7298', 'product_id': (13, 8)},
{'id': 4, 'external_id_client': '3', 'product_id': (12, 9)},
{'id': 5, 'external_id_client': '4', 'product_id': (12)}]
How can I do this?

You can use itertools.groupby for this:
from itertools import groupby
lst = [
{'id': 3, 'external_id_client': '7298', 'product_id': 13},
{'id': 3, 'external_id_client': '7298', 'product_id': 8},
{'id': 4, 'external_id_client': '3', 'product_id': 12},
{'id': 4, 'external_id_client': '3', 'product_id': 9},
{'id': 5, 'external_id_client': '4', 'product_id': 12}
]
result = [
{
"id": itemid,
"external_id_client": client,
"product_id": tuple(item["product_id"] for item in group)
}
for (itemid, client), group
in groupby(lst, key = lambda item: (item["id"], item["external_id_client"]))
]

Related

list of dicts- get the number of duplications [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a list of dicts (same format) like this :
L = [
{'id': 1, 'name': 'john', 'age': 34},
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
{'id': 2, 'name': 'hanna', 'age': 30},
{'id': 3, 'name': 'stack', 'age': 40}
]
I want to remove duplication and get the number of this duplication like this
[
{'id': 1, 'name': 'john', 'age': 34, 'duplication': 2},
{'id': 2, 'name': 'hanna', 'age': 30, 'duplication': 2},
{'id': 3, 'name': 'stack', 'age': 40, 'duplication': 1}
]
I already managed to remove the duplication by using a set.... but I can't get the number of duplications
my code :
no_duplication = [dict(s) for s in set(frozenset(d.items()) for d in L)]
no_duplication = [
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
{'id': 3, 'name': 'stack', 'age': 40}
]
Here is a solution you can give a try using collections.Counter,
from collections import Counter
print([
{**dict(k), "duplicated": v}
for k, v in Counter(frozenset(i.items()) for i in L).items()
])
[{'age': 34, 'duplicated': 2, 'id': 1, 'name': 'john'},
{'age': 30, 'duplicated': 2, 'id': 2, 'name': 'hanna'},
{'age': 40, 'duplicated': 1, 'id': 3, 'name': 'stack'}]
ar = [
{'id': 1, 'name': 'john', 'age': 34},
{'id': 1, 'name': 'john', 'age': 34},
{'id': 2, 'name': 'hanna', 'age': 30},
{'id': 2, 'name': 'hanna', 'age': 30},
{'id': 3, 'name': 'stack', 'age': 40}
]
br = []
cnt = []
for i in ar:
if i not in br:
br.append(i)
cnt.append(1)
else:
cnt[br.index(i)] += 1
for i in range(len(br)):
br[i]['duplication'] = cnt[i]
The desired output is contained in br as:
[
{'id': 1, 'name': 'john', 'age': 34, 'duplication': 2},
{'id': 2, 'name': 'hanna', 'age': 30, 'duplication': 2},
{'id': 3, 'name': 'stack', 'age': 40, 'duplication': 1}
]

Sort list of dicts by specific list of values

I'm trying to code a faster way to solve the following problem but I don't know how to do it:
I have the following list of dicts and list of identifiers:
list_of_dicts = [{'id': 1, 'name': 'A'}, {'id': 2, 'name': 'B'}, {'id': 3, 'name': 'C'}, {'id': 4, 'name': 'D'}]
list_of_ids = [1, 3, 2, 4, 1, 3, 4]
I'd like to have the following output:
[{'id': 1, 'name': 'A'}, {'id': 3, 'name': 'C'}, {'id': 2, 'name': 'B'}, {'id': 4, 'name': 'D'}, {'id': 1, 'name': 'A'}, {'id': 3, 'name': 'C'}, {'id': 4, 'name': 'D'}]
The way I'm doing it is:
list_of_dict_ids = [d['id'] for d in list_of_dicts]
ordered_list_by_ids = [list_of_dicts[list_of_dict_ids.index(i)] for i in list_of_ids]
Is there any faster way to do it?
You can do like this :
dic = {d["id"]: d for d in list_of_dicts}
dic
>>>{1: {'id': 1}, 2: {'id': 2}, 3: {'id': 3}, 4: {'id': 4}}
lst =[dic[i] for i in list_of_ids]
lst
>>>[{'id': 1}, {'id': 3}, {'id': 2}, {'id': 4}, {'id': 1}, {'id': 3}, {'id': 4}]

Specific Sort of elements to add in new list Python/Django

I have this :
[
[{ 'position': 1, 'user_id': 2, 'value': 4, 'points': 100}],
[{ 'position': 2, 'user_id': 6, 'value': 3, 'points': 88}],
[{ 'position': 3, 'user_id': 5, 'value': 2, 'points': 77}],
[{ 'position': 4, 'user_id': 7, 'value': 1, 'points': 66}],
[{ 'position': 5, 'user_id': 3, 'value': 1, 'points': 9}],
[{ 'position': 6, 'user_id': 11, 'value': 0, 'points': 9}],
[{ 'position': 7, 'user_id': 1, 'value': 0, 'points': 3}],
[{ 'position': 8, 'user_id': 10, 'value': 0, 'points': 3}],
[{ 'position': 9, 'user_id': 4, 'value': 0, 'points': 2}],
[{ 'position': 10, 'user_id': 8, 'value': 0, 'points': 2}]
]
is organized by points.
The idea is to choose the user_id and generate a new list with the selected 5 users.
Example:
user_id=3:
[{ 'position': 3, 'user_id': 5, 'value': 2, 'points': 77}],
[{ 'position': 4, 'user_id': 7, 'value': 1, 'points': 66}],
[{ 'position': 5, 'user_id': 3, 'value': 1, 'points': 9}],
[{ 'position': 6, 'user_id': 11, 'value': 0, 'points': 9}],
[{ 'position': 7, 'user_id': 1, 'value': 0, 'points': 3}]
It returns user_id 3 in the middle with 2 users hight and 2 users lower
user_id=2
[{ 'position': 1, 'user_id': 2, 'value': 4, 'points': 100}],
[{ 'position': 2, 'user_id': 6, 'value': 3, 'points': 88}],
[{ 'position': 3, 'user_id': 5, 'value': 2, 'points': 77}],
[{ 'position': 4, 'user_id': 7, 'value': 1, 'points': 66}],
[{ 'position': 5, 'user_id': 3, 'value': 1, 'points': 9}],
As user_id hasn't higher users it returns 4 lower users. So is always same logic.
user_id=9:
[{ 'position': 6, 'user_id': 11, 'value': 0, 'points': 9}],
[{ 'position': 7, 'user_id': 1, 'value': 0, 'points': 3}],
[{ 'position': 8, 'user_id': 10, 'value': 0, 'points': 3}],
[{ 'position': 9, 'user_id': 4, 'value': 0, 'points': 2}],
[{ 'position': 10, 'user_id': 8, 'value': 0, 'points': 2}]
on user_id=9 We only have 1 user lower so we add 3 higher users
If for example we just have 2 users in list, it should return that 2 users.
Main rules:
If we have 5 users or more, as to return 5 users.
if we have 4 users, as to return 4 users
How is a good way to do it?
thanks
This is basically only an update of my answer to your original question.
a = [
[{ 'position': 1, 'user_id': 2, 'value': 4, 'points': 100}],
[{ 'position': 2, 'user_id': 6, 'value': 3, 'points': 88}],
[{ 'position': 3, 'user_id': 5, 'value': 2, 'points': 77}],
[{ 'position': 4, 'user_id': 7, 'value': 1, 'points': 66}],
[{ 'position': 5, 'user_id': 3, 'value': 1, 'points': 9}],
[{ 'position': 6, 'user_id': 11, 'value': 0, 'points': 9}],
[{ 'position': 7, 'user_id': 1, 'value': 0, 'points': 3}],
[{ 'position': 8, 'user_id': 10, 'value': 0, 'points': 3}],
[{ 'position': 9, 'user_id': 4, 'value': 0, 'points': 2}],
[{ 'position': 10, 'user_id': 8, 'value': 0, 'points': 2}]
]
# Sort it if not already sorted
# a.sort(key=lambda x: x[0]['position'])
def find_index(l, user_id):
i = 0
while l[i][0]['user_id'] != user_id:
i += 1
return i
def get_subset(l, i):
return l[:(i + 1 + max(2, 4 - i))][-5:]
get_subset(a, find_index(a, 3))

Couldn't rename field in python list (key, value structure)

I have below list with nested lists (sort of key,values)
inp1=[{'id': 0, 'name': 98, 'value': 9}, {'id': 1, 'name': 66, 'value': 8}, {'id': 2, 'name': 29, 'value': 5}, {'id': 3, 'name': 99, 'value': 3}, {'id': 4, 'name': 15, 'value': 9}]
Am trying to replace 'name' with 'wid' and 'value' with 'wrt', how can I do it on same list?
My output should be like
inp1=[{'id': 0, 'wid': 98, 'wrt': 9}, {'id': 1, 'wid': 66, 'wrt': 8}, {'id': 2, 'wid': 29, 'wrt': 5}, {'id': 3, 'wid': 99, 'wrt': 3}, {'id': 4, 'wid': 15, 'wrt': 9}]
I tried below, but it doesn't work as list cannot be indexed with string but integer
inp1['name'] = inp1['wid']
inp1['value'] = inp1['wrt']
I tried if I can find any examples, but mostly I found only this for dictionary and not list.
You need to iterate each item, and remove the old entry (dict.pop is handy for this - it removes an entry and return the value) and assign to new keyes:
>>> inp1 = [
... {'id': 0, 'name': 98, 'value': 9},
... {'id': 1, 'name': 66, 'value': 8},
... {'id': 2, 'name': 29, 'value': 5},
... {'id': 3, 'name': 99, 'value': 3},
... {'id': 4, 'name': 15, 'value': 9}
... ]
>>>
>>> for d in inp1:
... d['wid'] = d.pop('name')
... d['wrt'] = d.pop('value')
...
>>> inp1
[{'wid': 98, 'id': 0, 'wrt': 9},
{'wid': 66, 'id': 1, 'wrt': 8},
{'wid': 29, 'id': 2, 'wrt': 5},
{'wid': 99, 'id': 3, 'wrt': 3},
{'wid': 15, 'id': 4, 'wrt': 9}]
def f(item):
if(item.has_key('name') and not item.has_key('wid')):
item['wid']=item.pop('name')
if(item.has_key('value') and not item.has_key('wrt')):
item['wrt']=item.pop('value')
map(f,inp1)
Output:
[{'wrt': 9, 'wid': 98, 'id': 0}, {'wrt': 8, 'wid': 66, 'id': 1}, {'wrt': 5, 'wid': 29, 'id': 2}, {'wrt': 3, 'wid': 99, 'id': 3}, {'wrt': 9, 'wid': 15, 'id': 4}]

python: iterate through list and replace elements with corresponding dictionary values

I am trying to replace list element value with value looked up in dictionary how do I do that?
list = [1, 3, 2, 10]
d = {'id': 1, 'val': 30},{'id': 2, 'val': 53}, {'id': 3, 'val': 1}, {'id': 4, 'val': 9}, {'id': 5, 'val': 2}, {'id': 6, 'val': 6}, {'id': 7, 'val': 11}, {'id': 8, 'val': 89}, {'id': 9, 'val': 2}, {'id': 10, 'val': 4}
for i in list:
for key, v in d.iteritems():
???
???
so at the end I am expecting:
list = [30, 1, 53, 4]
thank you
D2 = dict((x['id'], x['val']) for x in D)
L2 = [D2[x] for x in L]
td = (
{'val': 30, 'id': 1},
{'val': 53, 'id': 2},
{'val': 1, 'id': 3},
{'val': 9, 'id': 4},
{'val': 2, 'id': 5},
{'val': 6, 'id': 6},
{'val': 11, 'id': 7},
{'val': 89, 'id': 8},
{'val': 2, 'id': 9},
{'val': 4, 'id': 10}
)
source_list = [1, 3, 2, 10]
final_list = []
for item in source_list:
for d in td:
if d['id'] == item:
final_list.append(d['val'])
print('Source : ', source_list)
print('Final : ', final_list)
Result
Source : [1, 3, 2, 10]
Final : [30, 1, 53, 4]

Categories

Resources