How can I split keys of dictionary values using the comma? - python

The dictionary value mentioned below
dict = {'Incident, INC': 'Incident',
'SR, Service Request': 'Service Request',
'RCA ticket, Task': 'Problem',
'OCM, Alert': 'Change',
'Change': 'Minor enhancements'}
I need to map the dictionary values this
expect dictionary = {
'INC': 'Incident',
'Incident': 'Incident',
'Inc': 'Incident',
'Change': 'Minor Enchancement',
'SR': 'ServiceRequest',
'ServiceRequest': 'ServiceRequest'
}
I need to add more one condition given dictionary would the read given list if any of the value match it should the dict value or else it should return like Unspecified.
input ---->
new_list= ['Incident','INC','SR','Change','ABC']
Expected output --->
new_list = ['Incident','Incident','Minor Enchancement','Service Request','others']
My code does not work.
reversed_dict = {}
for key in my_dict:
items = [x.strip() for x in my_dict[key].split()]
for i in items:
reversed_dict[i] = key

Is this what you're looking for?
rdict = {}
for key in dict:
if any(list(map(lambda char: char in key, ','))):
k = key.split(',')
else: k = [key]
for i in k:
rdict[i.strip()] = dict[key]
print (rdict)
Returns:
{' Alert': 'Change',
' INC': 'Incident',
' Service Request': 'Service Request',
' Task': 'Problem',
'Change': 'Minor enhancements',
'Incident': 'Incident',
'OCM': 'Change',
'RCA ticket': 'Problem',
'SR': 'Service Request'}

Related

Remove dictionary if we have value is nan/None/NaN/null for key using python

I want to remove dictionary if value is nan/None/NaN/null for key . If nan is the only value in case of phone_type work then remove full dictionary itself.
Input Data
dic = ['Customer_Number': 12345,'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [1217]}, {'Phone_Type': 'work', 'Phone': [nan]}]]
Expected Output Data
dic = ['Customer_Number': 12345,'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [1217]}]]
code tried
#define which elements you want to remove:
to_be_deleted = [[], {}, "", None, "nan",nan, "NaN", NaN]
def remove_empty_elements(jsonData):
if isinstance(jsonData, list):
print("jsonDAta:", jsonData)
jsonData = [remove_empty_elements(elem) for elem in jsonData
if remove_empty_elements(elem) not in to_be_deleted]
elif isinstance(jsonData, dict):
jsonData = {key: remove_empty_elements(value) for key, value in jsonData.items()
if remove_empty_elements(value) not in to_be_deleted}
if len(jsonData) == 1:
return None
return jsonData
res = remove_empty_elements(dic)
Example solution:
to_delete = [ None, [None], "", None, "nan", "NaN"]
dic = {'Customer_Number': 12345,'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [1217]}, {'Phone_Type': 'work', 'Phone': [None]}]}
for key in dic.keys():
if isinstance(dic[key], list):
for item in dic[key]:
if isinstance(item, dict):
item_keys = list(item.keys())
for key_2 in item_keys:
if item[key_2] in to_delete:
del item[key_2]
output
{'Customer_Number': 12345, 'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [1217]}, {'Phone_Type': 'work'}]}
Change accordingly in order to catch more cases. This one only works if you have a list of dictionaries as value for the top level keys

Trying to return all nested json dictionary values that contain a substring python

I want to return a list of all of the text that comes after the nested 'en' key and before the comma that separates that key from the rest of the dictionary
dictionary = {'title': 'a',
'labels': {'label0': {'en': 'Statement',
'ca': 'dog1',
'bd': 'ေမးခြန္း ၁၀'},
'label1': {'en': 'Hello how are you',
'ca': 'cat6979309',
'bd': 'turkey89'},
'option0': {'en': 'No',
'bd': 'turkey232',
'ca': 'dog2'},
'option1': {'en': 'Absoluelty not',
'bd': 'turkey3',
'ca': 'dog3'},
'option2': {'en': 'Neutral ', 'bd': 'snake3', 'ca':'bat1'},
'option3': {'en': 'Somewhat Disagree',
'bd': 'turkey4',
'ca': 'dog4'},
'option4': {'en': 'For Sure',
'bd': 'turkey5',
'ca': 'dog5'}},```
I have tried this which isn't working, the goal is to have the function return something like sentences = ['Statement', 'Hello how are you', 'No', 'Absolutely not','Neutral'...]
def json(dic):
sentences = []
for (key, value) in dic.items():
if "en" in value:
sentences.append(value)
return sentences
You will need to recurse through the nested structure. Here is a generator function that does so:
def get_key(data, key):
if key in data:
yield data[key]
for k, v in data.items():
if isinstance(v, dict):
yield from get_key(v, key)
>>> list(get_key(dictionary, "en"))
['Statement', 'Hello how are you', 'No', 'Absoluelty not', 'Neutral ',
'Somewhat Disagree', 'For Sure']

Removing mulitple key items with dict comprehension python

I want to remove multiple key from my json and I'm using dictionary comprehensions like this
remove_key = ['name', 'description']
data = {'full-workflow': {'task_defaults': {'retry': {'count': 3, 'delay': 2}}, 'tasks': {'t1': {'action': 'nn.postgres.export-db', 'description': 'Dump prod DB with default settings', 'input': {'database': 'prod', 'filepath': '/var/tmp/prod-dump.pgsql', 'host': 'postgres.local', 'password': 'mypass', 'username': 'myuser'}, 'name': 'db export', 'on-success': ['t2']}, 't2': {'action': 'nn.aws.upload-to-s3', 'description': 'Upload to S3 bucket for development', 'input': {'sourcepath': '{{ tasks(t1).result.filepath }}', 'targetpath': 's3://mybucket/prod-dump.pgsql'}, 'name': 'Upload to S3', 'on-success': ['t3'], 'retry': {'count': 5, 'delay': 5}}, 't3': {'action': 'nn.shell.command', 'description': 'Remove temp file from batch folder ', 'input': {'cmd': 'rm {{ tasks(t1).result.filepath }}'}, 'name': 'Remove temp file', 'on-complete': ['t4']}, 't4': {'action': 'nn.notify.send-mail', 'description': 'Send email to admin containing target path', 'input': {'from': 'bot#nn.io', 'message': 'DB Dump {{ tasks(t1).result.filepath }} was stored to S3', 'subject': 'Prod DB Backup', 'to': 'admin#nn.io'}, 'name': 'Send email', 'target': 'nn'}}}, 'version': '2'}
def remove_additional_key(data):
return {
key: data[key] for key in data if key not in remove_key
}
then just
new_data = remove_additional_key(data)
Because this is nested dict, I want to remove_key from tasks dict, so what am I doing wrong?
Your data are nested dictionaries. If you want to remove any data with a key contained in remove_key, then I suggest a recursive approach. This can be achieved, based on your exiting function remove_additional_key, with ease:
def remove_additional_key(data):
sub_data = {}
for key in data:
if key not in remove_key:
if isinstance(data[key], dict): # if is dictionary
sub_data[key] = remove_additional_key(data[key])
else:
sub_data[key] = data[key]
return sub_data
new_data = remove_additional_key(data)
Note, if a entry is a dictionary can be checked by isinstance(data[key], dict). See How to check if a variable is a dictionary in Python?
You have a dictionary with a few nested dictionaries. If you know exactly which subdictionary you have those keys to remove, you can use:
data['full-workflow']['tasks']['t1'].pop('name')
Using the lookup approach (key: data[key]) in a dictionary comprehension is inefficient however, on such a small data amount you won't notice a difference.
If you don't know the exact path to your nested dictionary, you can use a function (posting another answer for your convenience)
def delete_keys_from_dict(d, lst_keys):
for k in lst_keys:
try:
del dict_del[k]
except KeyError:
pass
for v in dict_del.values():
if isinstance(v, dict):
delete_keys_from_dict(v, lst_keys)
return dict_del
Then you could call
delete_keys_from_dict(data, ['name', 'description'])
Needless to say, should you have name key in multiple nested dictionaries, all of them would be deleted, so be careful.

KeyError: 'name' Why can't I use 'name'?

I wanna make a dictionary has name's key & data.In views.py I wrote
data_dict ={}
def try_to_int(arg):
try:
return int(arg)
except:
return arg
def main():
book4 = xlrd.open_workbook('./data/excel1.xlsx')
sheet4 = book4.sheet_by_index(0)
data_dict_origin = OrderedDict()
tag_list = sheet4.row_values(0)[1:]
for row_index in range(1, sheet4.nrows):
row = sheet4.row_values(row_index)[1:]
row = list(map(try_to_int, row))
data_dict_origin[row_index] = dict(zip(tag_list, row))
if data_dict_origin['name'] in data_dict:
data_dict[data_dict_origin['name']].update(data_dict_origin)
else:
data_dict[data_dict_origin['name']] = data_dict_origin
main()
When I printed out data_dict,it is
OrderedDict([(1, {'user_id': '100', 'group': 'A', 'name': 'Tom', 'dormitory': 'C'}), (2, {'user_id': '50', 'group': 'B', 'name': 'Blear', 'dormitory': 'E'})])
My ideal dictionary is
dicts = {
Tom: {
'user_id': '100',
'group': 'A',
'name': 'Tom',
'dormitory': 'C'
},
Blear: {
},
}
How should I fix this?What should I write it?
The code is using the wrong key in the dictionary. The keys are 1, 2, and do not have the name key. You can use this code instead:
for value in data_dict.values():
if value['name'] in data_dict:
data_dict[value['name']].update(value)
else:
data_dict[value['name']] = value
Your data_dict_origin has numbers as keys and dicts as values (which technically makes it a sparse array of dicts). The "name" key exists in those dicts, not in your data_dict.

Create a key in a nested dict without using update function

Input :
{'Name': 'A','Blood Group': 'O +ve', 'Age': '1', 'Sex': 'M','Phone Number': '01234567', 'Mobile Number': '9876543210', 'Date of Birth': '01-01-95'}
1.
d.update({'Contact Info': {'Mobile Number':d['Mobile Number'],'Phone
Number':d['Phone Number'] }})
2.
d['Contact Info']={}
d['Contact Info']['Mobile Number']=d['Mobile Number']
Can you say any better way or different way to create a dictionary key which can be assigned to a dict item as value???
Original Code:
import csv
import copy
from collections import namedtuple
d={}
ls=[]
def nest():
with open ("details.csv", 'r') as f:
reader=csv.DictReader(f)
for row in reader:
d.update(row)
PersonalDetails = namedtuple('PersonalDetails','blood_group age sex')
ContactInfo = namedtuple('ContactInfo','phone_number mobile_number')
d1=copy.deepcopy(d)
ls.append(d1)
print ls
nest()
This is how I would update my dict of dicts:
I would create a function that will take a 3 arguments(The key of the subdict, the subkey of said subdict and the value you want to change.) I assign to be updated and then update that value.
d = {
'Name': 'A',
'Personal Details': {'Blood Group': 'O +ve', 'Age': '1', 'Sex': 'M'},
'Contact Info': {'Phone Number': '01234567', 'Mobile Number': '9876543210'},
'Date of Birth': '01-01-95'
}
def updateInfo(toBeUpdated, subkey, ValueToUpdate):
if toBeUpdated in d:
tempdict = d[toBeUpdated]
tempdict[subkey] = ValueToUpdate
d[toBeUpdated] = tempdict
print (d)
else:
print ("No %s to update" % (toBeUpdated))
updateInfo('Contact Info','Mobile Number','999 999 9999')
the result I get from this:
{'Name': 'A', 'Personal Details': {'Blood Group': 'O +ve', 'Age': '1', 'Sex': 'M'}, 'Contact Info': {'Phone Number': '01234567', 'Mobile Number': '999 999 9999'}, 'Date of Birth': '01-01-95'}

Categories

Resources