Sum values from nested dictionary - Python - python

Say I have the following dictionary
counts =
{(0, 0): {'00': 0, '01': 4908, '10': 0, '11': 5092},
(0, 1): {'00': 0, '01': 5023, '10': 0, '11': 4977},
(1, 0): {'00': 0, '01': 5058, '10': 0, '11': 4942},
(1, 1): {'00': 0, '01': 4965, '10': 0, '11': 5035}}
and I want to sum counts[0,0] and counts [0, 1]to get
idealcounts = {'00': 0, '01': 9931, '10': 0, '11': 10069}
How do I extract the counts[0,r] values and then sum them all up?
Thank you for your help.

You can just use collections.Counter and update it with the sub-dictionaries you want:
from collections import Counter
data = {
(0, 0): {'00': 0, '01': 4908, '10': 0, '11': 5092},
(0, 1): {'00': 0, '01': 5023, '10': 0, '11': 4977},
(1, 0): {'00': 0, '01': 5058, '10': 0, '11': 4942},
(1, 1): {'00': 0, '01': 4965, '10': 0, '11': 5035}
}
counts = Counter()
for k in ((0, 0), (0, 1)):
counts.update(Counter(data[k]))
print(counts)
# Counter({'00': 0, '01': 9931, '10': 0, '11': 10069})

Related

How to create a dictionary of permutation values?

I need to assign 0 and 1 as values to keys in the dictionaries:
combinations_string_list = [num_list_to_str(i) for i in itertools.product([0, 1], repeat=2)]
all_stategy = []
for i in range(16):
strategy_table = {x: y for x in combinations_string_list for y in [0, 1]}
all_stategy.append(strategy_table)
print(all_stategy)
I got [{'00': 1, '01': 1, '10': 1, '11': 1}, {'00': 1, '01': 1, '10': 1, '11': 1}, {'00': 1, '01': 1, '10': 1, '11': 1}, ...]
but I need [{'00': 0, '01': 0, '10': 0, '11': 0}, {'00': 0, '01': 0, '10': 0, '11': 1}, {'00': 0, '01': 0, '10': 1, '11': 0}, ...] instead.
How can I create this kind of value? Thanks!
You can zip the key sequence ["0", "1"] with each element of the cartesian product to produce input to dict:
>>> [dict(zip(["0", "1"], x)) for x in product([0,1], repeat=2)]
[{'0': 0, '1': 0}, {'0': 0, '1': 1}, {'0': 1, '1': 0}, {'0': 1, '1': 1}]
or
>>> values=[0,1]
>>> [dict(zip(map(str, values), x)) for x in product(values, repeat=2)]

How to transform this 2D list to a dictionary inside a dictionary?

I have been trying to transform the 2D list full_info_results but doesn't seem to find a good way to do it.
full_info_results = [
['*', 'G02', 'G05', 'G07', 'G08', 'G10'],
['P001', '1', '0', '-1', '503', '1'],
['P067', '1', '1', '0', '-1', '503'],
['P218', '0', '1', '1', '-1', '-1'],
['P101', '0', '0', '1', '1', '503'],
['P456', '1', '1', '-1', '1', '-1']
]
GXX = game_id
PXXX = player_id
1 = win
0, -1, 503 = these 3 number doesn't mean anything.
For each player_id I want to record whether they win each particular game_id or not. For example, Player id P001 win G02 and G10. If they win, I want to record it in dictionary as 1, if not win = 0.
Expected results:
expected_result = {'P001':{'G02':1, 'G05':0, 'G07':0, 'G08':0, 'G10':1},
'P067':{'G02':1, 'G05':1, 'G07':0, 'G08':0, 'G10':0},
'P218':{'G02':0, 'G05':1, 'G07':1, 'G08':0, 'G10':0},
'P101':{'G02':0, 'G05':0, 'G07':1, 'G08':1, 'G10':0},
'P456':{'G02':1, 'G05':1, 'G07':0, 'G08':1, 'G10':0}}
I wonder what is the proper way to transform this, because I hardly think of any right now.. Thanks in advance.
You could use the combination of dict and zip to build your sub dictionaries:
import pprint
expected_result = {}
# remove first item from full_info_results, to build the sub dict's keys
keys = full_info_results.pop(0)[1:]
for result in full_info_results:
# first item is the key, so remove it from the list
key = result.pop(0)
# convert '1' to 1 everything else to 0
expected_result[key] = dict(
zip(keys, [1 if r == '1' else 0 for r in result])
)
pprint.pprint(expected_result)
Out:
{'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1},
'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0},
'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0},
'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0},
'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}
keys= full_info_results[0][1:]
d = { l[0]: {k:(1 if v == '1' else 0) for k,v in zip(keys, l[1:])} for l in full_info_results[1:]}
In [18]: d
Out[18]:
{'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1},
'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0},
'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0},
'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0},
'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}
Try this :
full_info_results = [
['*', 'G02', 'G05', 'G07', 'G08', 'G10'],
['P001', '1', '0', '-1', '503', '1'],
['P067', '1', '1', '0', '-1', '503'],
['P218', '0', '1', '1', '-1', '-1'],
['P101', '0', '0', '1', '1', '503'],
['P456', '1', '1', '-1', '1', '-1']
]
keys = [key for key in full_info_results[0] if key.startswith("G")]
expected_result = {}
for pi in full_info_results[1:]:
expected_result[pi[0]] = dict(zip(keys, [1 if win == '1' else 0 for win in pi[1:]]))
print(expected_result)
Output:
{'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1},
'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0},
'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0},
'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0},
'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}
The following is rather concise using starred assignment excessively:
(_, *keys), *data = full_info_results
{head: {k: int(v=="1") for k, v in zip(keys, tail)} for head, *tail in data}
# {'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1},
# 'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0},
# 'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0},
# 'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0},
# 'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}

Printing possible substring in set of 3 and matching with dictionary keys in python

I want to print possible substring in set of 3 and assign dictionary values if pattern matches with dictionary.keys() and store them into new dictionary
input:
dict1={'000': 0, '001': 0, '010': 0, '011': 0, '100': 1, '101': 0, '110': 1, '111': 0}
str1=['010110100']
output:
sub_string= [010,101,011...]
new dict= {'010':0, '101':0, '011':0, '110':1......}
try this:
[str1[0][i:i+3] for i in range(len(str1[0])-2) if str1[0][i:i+3] in dict1]
{str1[0][i:i+3] : dict1.get(str1[0][i:i+3]) for i in range(len(str1[0])-2) if str1[0][i:i+3] in dict1}
# ['010', '101', '011', '110', '101', '010', '100']
# {'010': 0, '101': 0, '011': 0, '110': 1, '100': 1}
You could do like this:
dict1={'000': 0, '001': 0, '010': 0, '011': 0, '100': 1, '101': 0, '110': 1, '111': 0}
str1= '010110100'
sub_string = []
d = {}
for i in range(len(str1)-2):
temp = str1[i:i+3]
sub_string.append(temp)
d[temp] = dict1.get(temp)
print(sub_string)
print(d)
['010', '101', '011', '110', '101', '010', '100']
{'010': 0, '101': 0, '011': 0, '110': 1, '100': 1}

increment value in dictionary based on key in another dictionary

I have a masterDict dictionary with keys "1" through "8" with values set to 0
{'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0}
I also have anotherDict that i'm using to find the key containing a value closest to another value (i'm doing this multiple times with different values).
An example of one of those other values would be value1 = 900
An example of anotherDict would be:
{'1': 74, '2': 938, '3': 28, '4': 10, '5': 100, '6': 33, '7': 45, '8': 99}
The code i'm using to find the value closest to value1 in anotherDict is:
closestValue1 = key, value = min(anotherDict.items(), key=lambda (_, v): abs(v - value1))
In this case, closestValue1 returns:
{'2': 938}
How do I take this and increment the key 2 value in masterDict by 1?
So, masterDict would then contain:
{'1': 0, '2': 1, '3': 0, '4': 0, '5': 0, '6':0, '7':0, '8': 0}
master_dict = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0}
another_dict = {'1': 74, '2': 938, '3': 28, '4': 10, '5': 100, '6': 33, '7': 45, '8': 99}
target_val = 900
target_key, _ = min(another_dict.items(), key=lambda x: abs(target_value-x[1]))
master_dict[target_key]+=1
print (master_dict)

Creating a dictionary with a list of names

I have this list of names: [Frank, Sam, Kevin, Jack]
Is it possible to create a dictionary using the names in the list to create something like this?
'Frank' : {'Sam': 0, 'Kevin': 0, 'Jack': 0},
'Sam' : {'Frank': 0, 'Kevin': 0, 'Jack': 0},
'Kevin' : {'Frank': 0, 'Sam': 0, 'Jack': 0}
'Jack' : {'Frank': 0, 'Sam': 0, 'Kevin': 0}
I want to know if it's possible to iterate through the list, pick the first name and then create a dictionary with it, with the other members in the list as keys and 0 as the default value. And then repeat it for the other elements in the list as well.
I was thinking of using something like this.
my_dynamic_vars = dict()
my_dynamic_vars.update({string: dict()})
Any help would be much appreciated.
You can use nested dictionary comprehensions:
>>> lst = ['Frank', 'Sam', 'Kevin', 'Jack']
>>> dct = {x:{y:0 for y in lst if y != x} for x in lst}
>>> dct
{'Frank': {'Kevin': 0, 'Sam': 0, 'Jack': 0}, 'Kevin': {'Frank': 0, 'Jack': 0, 'Sam': 0}, 'Sam': {'Frank': 0, 'Jack': 0, 'Kevin': 0}, 'Jack': {'Frank': 0, 'Kevin': 0, 'Sam': 0}}
>>>
>>> # Just to demonstrate
>>> from pprint import pprint
>>> pprint(dct)
{'Frank': {'Jack': 0, 'Kevin': 0, 'Sam': 0},
'Jack': {'Frank': 0, 'Kevin': 0, 'Sam': 0},
'Kevin': {'Frank': 0, 'Jack': 0, 'Sam': 0},
'Sam': {'Frank': 0, 'Jack': 0, 'Kevin': 0}}
>>>
ummm
d= {}
names = ["Frank","Sam","Kevin","Jack"]
for name in names:
d[name] = dict.fromkeys(set(names).difference([name]),0)
print d
is probably how I would do it ..
names = ['Frank', 'Sam', 'Kevin', 'Jack']
d = dict.fromkeys(names, 0)
names_dict = {}
for name in names:
temp = d.copy()
del temp[name]
names_dict.update([(name, temp)])
output:
>>> for d in names_dict:
>>> print d
'Frank': {'Jack': 0, 'Kevin': 0, 'Sam': 0}
'Sam': {'Frank': 0, 'Jack': 0, 'Kevin': 0}
'Kevin': {'Frank': 0, 'Jack': 0, 'Sam': 0}
'Jack': {'Frank': 0, 'Kevin': 0, 'Sam': 0}
names = ['Frank', 'Sam', 'Kevin', 'Jack']
d = {name: dict.fromkeys([x for x in names if x != name], 0)
for name in names}
from pprint import pprint
pprint(d)
Output
{'Frank': {'Jack': 0, 'Kevin': 0, 'Sam': 0},
'Jack': {'Frank': 0, 'Kevin': 0, 'Sam': 0},
'Kevin': {'Frank': 0, 'Jack': 0, 'Sam': 0},
'Sam': {'Frank': 0, 'Jack': 0, 'Kevin': 0}}

Categories