Compare 2 dictionary by value and drop some k, v python - python

I have 2 dictionary :
First = {'A': 0.0, 'B': 0.0, 'C': 0.0, 'D': 0.0, 'E': 0.0, 'F': 0.0, 'G': 0.0, 'H': 0.0, 'I': 0.0, 'J': 0.0, 'K': 0.47, 'L': 0.0, 'M': 0.0, 'N': 0.0, 'O': 0.0, 'P': 0.0, 'Q': 0.0, 'R': 0.0, 'S': 0.41, 'T': 0.0}
and
Second = {'A': 0.0, 'B': 0.0, 'C': 0.0, 'D': 0.0, 'E': 0.0, 'F': 0.0, 'G': 0.0, 'H': 0.0, 'I': 0.18, 'J': 0.0, 'K': 1.0, 'L': 0.0, 'M': 0.0, 'N': 0.0, 'O': 0.0, 'P': 0.0, 'Q': 0.0, 'R': 0.0, 'S': 0.32, 'T': 0.0}
I woud like to drop certain values in second dict with conditions :
1 - if value in second dict is < to value un first dict, drop the (k,v) to the second dict
2 - if value in second dict == 0.0 then drop the (k,v) to the second dict
Fanaly, we have result :
Second = {'I': 0.18,'K': 1.0, 'S': 0.32}
Here is my code :
for key, value in dict(Second).items():
for key, value in dict(First).items():
if Second[value] == First[value] :
del Second[key]
elif First[value]> Second[value]:
del Second[key]
Second
But dont work :
KeyError: 0.0
Can someone help me !

You can't lookup a value as a key to a dictionary. Also, your 2nd loop is causing you to loop through the Second dictionary after you've already cleared all the elements that you want to clear.
You're looking for something more like:
for key, value in dict(Second).items():
if Second[key] == First[key] :
del Second[key]
elif First[key]> Second[key]:
del Second[key]
or even better
for key, value in Second.items():
if Second[key] <= First[key]:
del Second[key]
though your second condition, specifies it should be something more like:
for key, value in Second.items():
if Second[key] < First[key] or Second[key] == 0.0:
del Second[key]

Iterate through the first dict, since it won't be the one changing, and based on your conditions filter stuff out
for k,v in First.iteritems():
if Second[k] < v or Second[k] == 0.0:
del Second[k]
output:
{'I': 0.18, 'K': 1.0}

You are mistakenly using the item value rather than the key in an attempt to access the value. Also, you will run into problems if you attempt to mutate the dict while iterating over it. Instead, you could use dict comprehension.
For example:
d1 = {'A': 0.0, 'B': 0.0, 'C': 0.0, 'D': 0.0, 'E': 0.0, 'F': 0.0, 'G': 0.0, 'H': 0.0, 'I': 0.0, 'J': 0.0, 'K': 0.47, 'L': 0.0, 'M': 0.0, 'N': 0.0, 'O': 0.0, 'P': 0.0, 'Q': 0.0, 'R': 0.0, 'S': 0.41, 'T': 0.0}
d2 = {'A': 0.0, 'B': 0.0, 'C': 0.0, 'D': 0.0, 'E': 0.0, 'F': 0.0, 'G': 0.0, 'H': 0.0, 'I': 0.18, 'J': 0.0, 'K': 1.0, 'L': 0.0, 'M': 0.0, 'N': 0.0, 'O': 0.0, 'P': 0.0, 'Q': 0.0, 'R': 0.0, 'S': 0.32, 'T': 0.0}
d2 = {k: v for k, v in d2.items() if v != 0 and v != d1[k]}
print(d2)
# {'I': 0.18, 'K': 1.0, 'S': 0.32}

Related

Sum of values from nested dictionary [duplicate]

This question already has answers here:
How to sum values in multidimensional dictionary?
(6 answers)
Closed 1 year ago.
I have this nested dictionary:
{'rekless': {'C': 2.0, 'H': 4.0, 'J': 0.0}, 'bwipo': {'C': 3.0, 'D': 4.0, 'H': 0.0}, 'wunder': {'D': 10.0, 'G': 20.0, 'H': 0.50}, 'caps': {'D': 3.1, 'I': 2.0, '9J': 1.0, '10K': 2.0}, 'jankos': {'D': 3.2, 'I': 2.0, 'J': 1.0, 'K': 2.0} }
Want i want to do is to loop through it and sum all values of the "C, H, J,", etc...
So like, 'C' is going to be (2.0 + 3.0), 'D' is going to be (4.0 + 10.0 + 3.1 + 3.2).
So i can end up with something similar to this:
{ C: 'sum of values from c', D: 'sum of values from d', etc.. }
It doesnt need to be in a dictionary the final result.
Any ideias on how to start?
You need to loop through both dictionary and subdictionary:
d = {
'rekless': {'C': 2.0, 'H': 4.0, 'J': 0.0},
'bwipo': {'C': 3.0, 'D': 4.0, 'H': 0.0},
'wunder': {'D': 10.0, 'G': 20.0, 'H': 0.50},
'caps': {'D': 3.1, 'I': 2.0, '9J': 1.0, '10K': 2.0},
'jankos': {'D': 3.2, 'I': 2.0, 'J': 1.0, 'K': 2.0}
}
summary = dict()
for key, subdict in d.items():
for k, v in subdict.items():
summary[k] = summary.get(k, 0) + v
print(summary)
Assume d is:
d = {'rekless': {'C': 2.0, 'H': 4.0, 'J': 0.0}, 'bwipo': {'C': 3.0, 'D': 4.0, 'H': 0.0}, 'wunder': {'D': 10.0, 'G': 20.0, 'H': 0.50}, 'caps': {'D': 3.1, 'I': 2.0, '9J': 1.0, '10K': 2.0}, 'jankos': {'D': 3.2, 'I': 2.0, 'J': 1.0, 'K': 2.0} }
Try this:
vals ={}
for k in d:
sum_ = 0
for k_ in d[k]:
if k_ in vals:
vals[k_]+= int(d[k][k_])
else:
vals[k_]= int(d[k][k_])
for k__ in vals:
vals[k__]='sum of values from {} is: {}'.format(k__, vals[k__])
which results in vals to be:
{'C': 'sum of values from C is: 5',
'H': 'sum of values from H is: 4',
'J': 'sum of values from J is: 1',
'D': 'sum of values from D is: 20',
'G': 'sum of values from G is: 20',
'I': 'sum of values from I is: 4',
'9J': 'sum of values from 9J is: 1',
'10K': 'sum of values from 10K is: 2',
'K': 'sum of values from K is: 2'}

How to sort dictionary by values in inner dictionary

{1: {'p_place': {'x': 0.65, 'y': 0.255, 'z': 0.279}, 'q_place': {'q0': 1.0, 'q1': 0.0, 'q2': 0.0, 'q3': 0.0}, 'offset_pick': {'x': (0.0,), 'y': (0.0,), 'z': (0.05,)}, 'centroid_upper': {'x': (0.65,), 'y': (0.2549999999999998,), 'z': (0.329,)}}, 2: {'p_place': {'x': 0.65, 'y': 0.255, 'z': 0.279}, 'q_place': {'q0': 1.0, 'q1': 0.0, 'q2': 0.0, 'q3': 0.0}, 'offset_pick': {'x': (0.0,), 'y': (0.0,), 'z': (0.05,)}, 'centroid_upper': {'x': (0.65,), 'y': (0.2549999999999998,), 'z': (0.329,)}}}
I have this kind of dictionary. I want to sort it by value Y and Z of p_place
Did you try anything at all?
newd = sorted(dct, key=lambda el: (el['p_place']['y'],el['p_place']['z']))

clustering a list of dictionaries according to specific key/value pairs

Say i have a list of dictionaries that all have the same keys. and i want to regroup these into several lists such that the values for certain attributes of my choosing are equal. here is an example:
Suppose i have the following list of dictionaries:
[ {'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.0, 'b': 0.2, 'c': 0.3},
{'a': 0.1, 'b': 0.7, 'c': 0.4},
{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]
and i want to cluster it according the the keys a and b. Then the output would be the following list of list of dictionaries:
[[{'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.0, 'b': 0.2, 'c': 0.3}]
[{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.1, 'b': 0.7, 'c': 0.4}]
[{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]]
What is the best way of achieving this?
Sort this firstly, then use itertools.groupby.You may could try this below:
from itertools import groupby
t = [{'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.0, 'b': 0.2, 'c': 0.3},
{'a': 0.1, 'b': 0.7, 'c': 0.4},
{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]
print([[*j] for i, j in groupby(sorted(t, key=lambda x: (x['a'], x['b'])), key=lambda x: (x['a'], x['b']))])
Result:
[[{'a': 0.0, 'b': 0.2, 'c': 0.1}, {'a': 0.0, 'b': 0.2, 'c': 0.3}], [{'a': 0.0, 'b': 0.7, 'c': 0.5}, {'a': 0.0, 'b': 0.7, 'c': 0.6}], [{'a': 0.1, 'b': 0.7, 'c': 0.2}, {'a': 0.1, 'b': 0.7, 'c': 0.4}]]
If you want to create a function to receive muitlple keys, you could try:
from itertools import groupby
def group_by(*args):
return [[*j] for i, j in groupby(sorted(t, key=itemgetter(*args)), key=itemgetter(*args))]
t = [{'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.0, 'b': 0.2, 'c': 0.3},
{'a': 0.1, 'b': 0.7, 'c': 0.4},
{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]
print(group_by('a', 'b'))

Changing the orientation of pyplot graph's x values

I have the following data:
stage_summary = {'Day1': {'1': 100.0, '2': 0.0, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day2': {'1': 100.0, '2': 0.0, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day3': {'1': 89.8, '2': 10.2, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day4': {'1': 58.6, '2': 41.4, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.0, '7': 0.0, '8': 0.0}, 'Day5': {'1': 52.71, '2': 47.06, '3': 0.0, '4': 0.0, '5': 0.0, '6': 0.23, '7': 0.0, '8': 0.0}, 'Day6': {'1': 47.89, '2': 50.65, '3': 0.0, '4': 0.0, '5': 0.0, '6': 1.46, '7': 0.0, '8': 0.0}, 'Day7': {'1': 49.72, '2': 46.95, '3': 0.0, '4': 0.0, '5': 0.0, '6': 3.33, '7': 0.0, '8': 0.0}, 'Day8': {'1': 52.59, '2': 39.35, '3': 0.0, '4': 0.0, '5': 0.0, '6': 8.05, '7': 0.0, '8': 0.01}, 'Day9': {'1': 55.45, '2': 30.73, '3': 0.0, '4': 0.0, '5': 0.0, '6': 13.74, '7': 0.0, '8': 0.08}}
date_list = ['2019-05-28',
'2019-05-29',
'2019-05-30',
'2019-05-31',
'2019-06-03',
'2019-06-04',
'2019-06-05',
'2019-06-06',
'2019-06-07']
And I plot the data in this way, as a line graph:
for each_state in list(range(1,9)):
tmp = []
for each_day in stage_summary:
tmp.append(stage_summary[each_day][str(each_state)])
plt.plot(date_list, tmp, label=str('State ' + str(each_state)))
plt.ylabel('Probability')
plt.xlabel('Days')
plt.legend(loc='best')
which results in the following graph,
However, as you can see, the x labels are dates, which are long strings. And therefore, it has to be rotated and be presented vertically to make it readable. But I am not sure how I can change the orientation of x values in a plotly graph. How can I do so?
You are using matplotlib and not plotly, right?
If yes, try:
for each_state in list(range(1,9)):
tmp = []
for each_day in stage_summary:
tmp.append(stage_summary[each_day][str(each_state)])
plt.plot(date_list, tmp, label=str('State ' + str(each_state)))
plt.ylabel('Probability')
plt.xlabel('Days')
plt.xticks(rotation=45)
plt.legend(loc='best')
Set plotly.graph_objs.Layout.xaxis.tickangle = 90.
Detail: https://plot.ly/python/axes/#set-and-style-axes-title-labels-and-ticks

Round off dict values to 2 decimals

I'm having a hard time rounding off values in dicts. What I have is a list of dicts like this:
y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c':
10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a':
80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
I need to round off the values to just 2 decimal places.
When I try the following:
def roundingVals_toTwoDeci(y):
for d in y:
for k, v in d.items():
v = ceil(v*100)/100.0
print v
d[k] = v
return
roundingVals_toTwoDeci(y)
s = json.dumps(y)
print s
I get:
0.0
0.0
18.2
0.0
27.3
54.5
0.0
0.0
0.0
[{"a": 0.0, "b": 0.0, "c": 27.300000000000001, "d": 0.0, "e": 54.5, "f": 0.0, "g": 18.199999999999999, "h": 0.0, "i": 0.0}]
I need to make this work with versions 2.4+ and so am not using dict comprehensions.
First, I am having a hard time looping through all the key, values in all the dicts in the original.
Second, this result has just 1 decimal point instead of 2 when it prints inside the function?
Third, why is the 'json.dumps' and then 'print' not showing the values from inside the function?
EDIT:
Working with #Mark Ransom's answer below, I get the desired o/p. However, I have to urlencode the json.dumps value and send it to a URL. At the URL, it decodes the values into all the decimal places. So, for example, if, josn.dumps gives {"a": 9.1}, the URL shows it (after urlencode) as 9.10034254344365. The modified code is as below:
class LessPrecise(float):
def __repr__(self):
return str(self)
def roundingVals_toTwoDeci(y):
for d in y:
for k, v in d.items():
v = LessPrecise(round(v, 2))
print v
d[k] = v
roundingVals_toTwoDeci(y)
j = json.dumps(y)
print j
params = urllib.urlencode({'thekey': j})
print json.dumps gives {"a": 9.1}
At the URL after urlencode, it gives 9.1078667322034 instead of 9.1as in the following:
Output:::
100.0
0.0
0.0
0.0
100.0
0.0
0.0
0.0
81.8
0.0
18.2
0.0
90.0
0.0
0.0
10.0
[{"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a":
81.8, "b": 0.0, "c": 18.2, "d": 0.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
At the URL:
9.100000381469727
The JSON string after json.dumps()
[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a":
80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
The urlencode string - after decoding at http://meyerweb.com/eric/tools/dencoder/
thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d":
0.0}, {"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
At the URL, I get values like 18.200000762939453(this value is from a later script run)
Taking the best bits from a couple of other answers:
class LessPrecise(float):
def __repr__(self):
return str(self)
def roundingVals_toTwoDeci(y):
for d in y:
for k, v in d.items():
v = LessPrecise(round(v, 2))
print v
d[k] = v
>>> roundingVals_toTwoDeci(y)
80.0
10.0
0.08
10.67
80.73
10.78
0.0
10.0
80.72
10.0
0.78
10.0
80.78
10.0
0.0
10.98
>>> s=json.dumps(y)
>>> s
'[{"a": 80.0, "c": 10.0, "b": 0.08, "d": 10.67}, {"a": 80.73, "c": 10.78, "b": 0.0, "d": 10.0}, {"a": 80.72, "c": 10.0, "b": 0.78, "d": 10.0}, {"a": 80.78, "c": 10.0, "b": 0.0, "d": 10.98}]'
JSONEncoder uses repr, and repr prints floats with all their available precision. The only possible solutions are to inherit from JSONEncoder and round while actually converting the values to a string (which implies to copy and adapt some code from the json.encoder module), or else wrap the floats into your own type RoundedFloat and register a serializer for that. Also note that repr's behaviour depends on the Python version used.
As often with non-obvious behaviour, the observation during debugging can trick you: print uses str(), and str() rounds at a certain point, unlike repr() which shows the naked ugliness of floating point maths.
The proof is in the code:
>>> class F(float):
... def __str__(self): return "str"
... def __repr__(self): return "repr"
...
...
>>> print F(1)
str
>>> F(1)
repr
>>> repr(1-1e-15)
'0.999999999999999'
>>> str(1-1e-15)
'1.0'
import json
y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c':
10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a':
80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
def roundingVals_toTwoDeci(y):
for d in y:
for k, v in d.items():
v = round(v,2) # <--- round() does exact that.
d[k] = v # <--- You need to put the rounded v back in d
print v
return
roundingVals_toTwoDeci(y)
s = json.dumps(y)
print s
Answering the second part of your question
Try replacing line 5 of your code with:
v = round(v, 2)
This will round the number to two decimal places. Using round, I get
[{'a': 80.0, 'c': 10.0, 'b': 0.08, 'd': 10.67}, {'a': 80.73, 'c': 10.78, 'b': 0.0, 'd': 10.0}, {'a': 80.72, 'c': 10.0, 'b': 0.78, 'd': 10.0}, {'a': 80.78, 'c': 10.0, 'b': 0.0, 'd': 10.98}]
I am using Python 2.7.2. Here's all the code:
from math import ceil
import json
y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903},
{'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0},
{'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0},
{'a': 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
def roundingVals_toTwoDeci(y):
for d in y:
for k, v in d.items():
v = round(v, 2)
#print v
d[k] = v
return
roundingVals_toTwoDeci(y)
s = json.dumps(y)
print s
I don't understand what relates to json, but I can propose:
from math import ceil
y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903},
{'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0},
{'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0},
{'a': 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
class TwoDec(float):
def __repr__(self):
return "%.2f" % self
def roundingVals_to_TwoDeci(y,ceil=ceil,TwoDec=TwoDec):
for d in y:
for k, v in d.iteritems():
d[k] = TwoDec(ceil(v*100)/100)
roundingVals_to_TwoDeci(y)
for el in y:
print el
result
{'a': 80.00, 'c': 10.00, 'b': 0.08, 'd': 10.68}
{'a': 80.74, 'c': 10.79, 'b': 0.00, 'd': 10.00}
{'a': 80.73, 'c': 10.00, 'b': 0.79, 'd': 10.00}
{'a': 80.79, 'c': 10.00, 'b': 0.00, 'd': 10.98}
I know this question is old, but here is a quick one-liner solution that works at least here in Linux Python 2.7.6 and might be interesting to someone else:
y = [{ x : round(z, 2) for x,z in yi.items()} for yi in y ]
However, this might be inefficient for larger data sets, as it re-generates the list/dict structure.
[[round(d[key],2) for key in d] for d in y]
[{key:round(d[key], 2) for key in d} for d in y]
Round dictionary values one-liner
with d as the dict and here to 2 decimal places
d = {k: round(v, 2) for k, v in d.items()}
Given the subject line Round off dict values to 2 decimals, this is what most will be coming here to find.
dictionary comprehension might be used in a search

Categories

Resources