Python str not list - python

I wanted to obtain array of int like [1,2,3]
but got [dict_values([1]),dict_values([2]),dict_values([3])]
My code:
taken_employee_ids_dict = [{'id': 1}, {'id': 2}, {'id': 3}]
taken_employee_ids = [str(taken_employee_ids_list.values()) for taken_employee_ids_list in taken_employee_ids_dict]
in_values_check = ','.join(str(id) for id in taken_employee_ids)
And then in_values_check should go in:
qry = "SELECT CONCAT(first_name, ',', surname) AS full_name, id \
FROM employees \
WHERE date_terminated IS NULL \
and id NOT IN " + "(" + in_values_check + ")" \
" ORDER BY first_name, surname ASC"
sorry i am new to python thanks

For a problem like this, a list comprehension is a commonly applied solution:
taken_employee_ids_dict = [{'id': 1}, {'id': 2}, {'id': 3}]
in_values_check = [employee["id"] for employee in taken_employee_ids_dict]
print(in_values_check)
This outputs
[1, 2, 3]

You can do it with lambda or list comprehension
With Lambda,
taken_employee_ids_dict = [{'id': 1}, {'id': 2}, {'id': 3}]
in_values_check = list(map(lambda x : x['id'], taken_employee_ids_dict))
print(in_values_check)
With List Comprehension,
taken_employee_ids_dict = [{'id': 1}, {'id': 2}, {'id': 3}]
in_values_check = [dic['id'] for dic in taken_employee_ids_dict]
print(in_values_check)
Output:
[1, 2, 3]

You can use something like this.
taken_employee_ids_dict = [{'id': 1}, {'id': 2}, {'id': 3}]
final_list = []
for ele in taken_employee_ids_dict:
for key,value in ele.items():
final_list.append(value)
print(final_list)

Try this:
taken_employee_ids = [str(employee["id"]) for employee in taken_employee_ids_dict]
in_values_check = ','.join(str(id) for id in taken_employee_ids)

Related

Python, sort dict based on external list

I have to sort a dict like:
jobs = {'elem_05': {'id': 'fifth'},
'elem_03': {'id': 'third'},
'elem_01': {'id': 'first'},
'elem_00': {'id': 'zeroth'},
'elem_04': {'id': 'fourth'},
'elem_02': {'id': 'second'}}
based on the "id" elements, whose order can be found in a list:
sorting_list = ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth']
The trivial way to solve the problem is to use:
tmp = {}
for x in sorting_list:
for k, v in jobs.items():
if v["id"] == x:
tmp.update({k: v})
but I was trying to figure out a more efficient and pythonic way.
I've been trying sorted and lambda functions as key, but I'm not familiar with that yet, so I was unsuccessful so far.
I would use a dictionary as key for sorted:
order = {k:i for i,k in enumerate(sorting_list)}
# {'zeroth': 0, 'first': 1, 'second': 2, 'third': 3, 'fourth': 4, 'fifth': 5}
out = dict(sorted(jobs.items(), key=lambda x: order.get(x[1].get('id'))))
output:
{'elem_00': {'id': 'zeroth'},
'elem_01': {'id': 'first'},
'elem_02': {'id': 'second'},
'elem_03': {'id': 'third'},
'elem_04': {'id': 'fourth'},
'elem_05': {'id': 'fifth'}}
There is a way to sort the dict using lambda as a sorting key:
jobs = {'elem_05': {'id': 'fifth'},
'elem_03': {'id': 'third'},
'elem_01': {'id': 'first'},
'elem_00': {'id': 'zeroth'},
'elem_04': {'id': 'fourth'},
'elem_02': {'id': 'second'}}
sorting_list = ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth']
sorted_jobs = dict(sorted(jobs.items(), key=lambda x: sorting_list.index(x[1]['id'])))
print(sorted_jobs)
This outputs
{'elem_00': {'id': 'zeroth'}, 'elem_01': {'id': 'first'}, 'elem_02': {'id': 'second'}, 'elem_03': {'id': 'third'}, 'elem_04': {'id': 'fourth'}, 'elem_05': {'id': 'fifth'}}
I have a feeling the sorted expression could be cleaner but I didn't get it to work any other way.
You can use OrderedDict:
from collections import OrderedDict
sorted_jobs = OrderedDict([(el, jobs[key]['id']) for el, key in zip(sorting_list, jobs.keys())])
This creates an OrderedDict object which is pretty similar to dict, and can be converted to dict using dict(sorted_jobs).
Similar to what is already posted, but with error checking in case id doesn't appear in sorting_list
sorting_list = ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth']
jobs = {'elem_05': {'id': 'fifth'},
'elem_03': {'id': 'third'},
'elem_01': {'id': 'first'},
'elem_00': {'id': 'zeroth'},
'elem_04': {'id': 'fourth'},
'elem_02': {'id': 'second'}}
def custom_order(item):
try:
return sorting_list.index(item[1]["id"])
except ValueError:
return len(sorting_list)
jobs_sorted = {k: v for k, v in sorted(jobs.items(), key=custom_order)}
print(jobs_sorted)
The sorted function costs O(n log n) in average time complexity. For a linear time complexity you can instead create a reverse mapping that maps each ID to the corresponding dict entry:
mapping = {d['id']: (k, d) for k, d in jobs.items()}
so that you can then construct a new dict by mapping sorting_list with the ID mapping above:
dict(map(mapping.get, sorting_list))
which, with your sample input, returns:
{'elem_00': {'id': 'zeroth'}, 'elem_01': {'id': 'first'}, 'elem_02': {'id': 'second'}, 'elem_03': {'id': 'third'}, 'elem_04': {'id': 'fourth'}, 'elem_05': {'id': 'fifth'}}
Demo: https://replit.com/#blhsing/WorseChartreuseFonts

how to insert list of elements into list of dictionaries

I have one list of elements and another list of dictionaries and i want to insert list of elements into each dictionary of list
list_elem = [1,2,3]
dict_ele = [{"Name":"Madhu","Age":25},{"Name":"Raju","Age:24},{""Name":"Mani","Age":12}],
OUTPUT As:
[{"ID":1,"Name":"Madhu","Age":25},{"ID":2,"Name":"Raju","Age:24},{"ID":3,"Name":"Mani","Age":12}]
I have tried this way :
dit = [{"id":item[0]} for item in zip(sam)]
# [{"id":1,"id":2,"id":3}]
dic1 = list(zip(dit,data))
print(dic1)
# [({"id":1},{{"Name":"Madhu","Age":25}},{"id":2},{"Name":"Raju","Age:24},{"id":3},{""Name":"Mani","Age":12})]
What is the most efficient way to do this in Python?
Making an assumption here that the OP's original question has a typo in the definition of dict_ele and also that list_elem isn't really necessary.
dict_ele = [{"Name":"Madhu","Age":25},{"Name":"Raju","Age":24},{"Name":"Mani","Age":12}]
dit = [{'ID': id_, **d} for id_, d in enumerate(dict_ele, 1)]
print(dit)
Output:
[{'ID': 1, 'Name': 'Madhu', 'Age': 25}, {'ID': 2, 'Name': 'Raju', 'Age': 24}, {'ID': 3, 'Name': 'Mani', 'Age': 12}]
dict_ele = [{"Name":"Madhu","Age":25},{"Name":"Raju","Age":24},{"Name":"Mani","Age":12}]
list_elem = [1,2,3]
[{'ID': id, **_dict} for id, _dict in zip(list_elem, dict_ele)]
[{'ID': 1, 'Name': 'Madhu', 'Age': 25}, {'ID': 2, 'Name': 'Raju', 'Age': 24}, {'ID': 3, 'Name': 'Mani', 'Age': 12}]
try this: r = [{'id':e[0], **e[1]} for e in zip(list_elem, dict_ele)]

merge dictionary and list of dictionaries in a specific way

Not working too much with dictionaries and thinking about it for too long. I'm looking for merging dictionaries in a way I'm describing a little lower. Dictionary might be a bit bigger.
Thanks!
dict1:
{'0': '5251', '1': '5259'}
list:
[{'id': 5259, 'name': 'chair'}, {'id': 5251, 'name': 'table'}]
Result:
{'0': {'id': 5251, 'name': 'table'}, '1': {'id': 5259, 'name': 'chair'}}
This should work:
dict1 = {'0': '5251', '1': '5259'}
ls = [{'id': 5259, 'name': 'chair'}, {'id': 5251, 'name': 'table'}]
idToEntry = dict([x['id'], x] for x in ls)
dict2 = dict([k, idToEntry[int(v)]] for k, v in dict1.items())
print(dict2)
The output:
{'0': {'id': 5251, 'name': 'table'}, '1': {'id': 5259, 'name': 'chair'}}
This solution will be somewhat slow for larger lists, but it is very simple:
result = {}
for key, val in dict1.items():
for dict2 in list1:
if dict2['id'] == val:
result['key'] = dict2
Construct a dictionary with reversed keys and values from your original:
d = {'0': '5251', '1': '5259'}
rev_dict = {v: k for k, v in d.items()}
Now you can use the id from each item of your list as an index into your dict
l = [{'id': 5259, 'name': 'chair'}, {'id': 5251, 'name': 'table'}]
merged_data = {rev_dict[str(x['id'])]: x for x in l}
# {'1': {'id': 5259, 'name': 'chair'}, '0': {'id': 5251, 'name': 'table'}}
d = {'0': '5251', '1': '5259'}
l = [{'id': 5259, 'name': 'chair'}, {'id': 5251, 'name': 'table'}]
# Lets prepare an intermediate dict for faster computation on large data
d1 = {str(x['id']): x for x in l}
merged_dict = {x: d1[y] for x, y in d.items()}
print(merged_dict)

Fastest method: for value in DictA, find value in DictB and retrieve other DictB value?

For each item in dictA, I want to search for it in dictB, if dictB has it then I want to pull some other values from dictB and add it to dictA.
An example that is working is here, however it is rather slow as I have 50,000+ items to search through and it will perform this similar function on multiple dicts.
Is there a fast method of performing this search?
dictA = [
{'id': 12345},
{'id': 67890},
{'id': 11111},
{'id': 22222}
]
dictB = [
{'id': 63351, 'name': 'Bob'},
{'id': 12345, 'name': 'Carl'},
{'id': 59933, 'name': 'Amy'},
{'id': 11111, 'name': 'Chris'}
]
for i in dictA:
name = None
for j in dictB:
if i['id'] == j['id']:
name = j['name']
i['name'] = name
The dictA output after this would be:
dictA = [
{'id': 12345, 'name': 'Carl'},
{'id': 67890, 'name': None},
{'id': 11111, 'name': 'Chris'},
{'id': 22222, 'name': None}
]
The given is list of dict. You can create dict from that assuming id is uninque. Converting from list of dict to dict will work for your case.
dictA = [
{'id': 12345},
{'id': 67890},
{'id': 11111},
{'id': 22222}
]
dictB = [
{'id': 63351, 'name': 'Bob'},
{'id': 12345, 'name': 'Carl'},
{'id': 59933, 'name': 'Amy'},
{'id': 11111, 'name': 'Chris'}
]
actual_dictB = dict()
for d in dictB:
actual_dictB[d['id']] = d['name']
for i in dictA:
i['name'] = actual_dictB.pop(i['id'], None) # now search have became O(1) constant. So best time complexity achived O(n) n=length of dictA
print(dictA)
Follow up for additional question:
actual_dictB = dict()
for d in dictB:
id_ = d['id']
d.pop('id')
actual_dictB[id_] = d
tmp = dict([(k,None) for k in dictB[0].keys() if k!='id'])
for i in dictA:
if i['id'] not in actual_dictB:
i.update(tmp)
else:
i.update(actual_dictB[i['id']])
print(dictA)

Why is data contamination?

# -*- coding: utf-8 -*-
import random, pprint
user = {}
USERINFO_STRUCT = {
'id': '',
}
def client_new(id):
global user
newuser = USERINFO_STRUCT
newuser['id'] = id
user[id] = newuser
pprint.pprint(user)
print ""
client_new(1)
client_new(2)
client_new(3)
I want results:
{1: {'id': 1}}
{1: {'id': 1}, 2: {'id': 2}}
{1: {'id': 1}, 2: {'id': 2}, 3: {'id': 3}}
The results of that code execution is:
{1: {'id': 1}}
{1: {'id': 2}, 2: {'id': 2}}
{1: {'id': 3}, 2: {'id': 3}, 3: {'id': 3}}
How are you doing this?
My machine is Debian Linux 8.6 (Python 2.7.9).
In python, dictionaries and lists are copied by reference:
>>> a = [0]
>>> b = a
>>> b
[0]
>>> a
[0]
>>> b[0] = 2
>>> a
[2]
>>> b
[2]
So, b and a refer to the same list in the python memory and modifying one modifies the other.
So, what you can do is when you want to create another copy of a list or dictionary, make a copy. For a list:
>>> b = a[:] # for a list
For a dictionary:
>>> b = a.copy() # for a dictionary
So, in your code, what you need is to copy the USERINFO_STRUCT into the newuser using newuser = USERINFO_STRUCT.copy()

Categories