Combining two lists of rows - python

I have two lists of rows, each with two rows, like this:
list1 = [{'a': 'foo', 'b': 'bar', 'c': 'baz'}, {'d': 'qux', 'e': 'quux', 'f': 'quuz'}]
list2 = [{'g': 'corge', 'h': 'grault', 'i': 'garply'}, {'j': 'waldo', 'k': 'fred', 'l': 'plugh'}]
I would like to join the two lists so that row 1 from list 1 is joined with row 1 from list 2 and row 2 from list 1 is joined with row 2 from list 2, like this:
final_list = [{'a': foo, 'b': bar 'c': baz, 'g': corge, 'h': grault, 'i': garply}, {'d': qux, 'e': quux, 'f': quuz, 'j': waldo, 'k': fred, 'l': plugh}]
I have tried:
final_list = [[i, j] for i,j in zip(list1, list2)]
But it doesn't quite join them correctly. Instead, it produces:
[{'a': foo, 'b': bar 'c': baz}, {'g': corge, 'h': grault, 'i': garply}], [{'d': qux, 'e': quux, 'f': quuz}, {'j': waldo, 'k': fred, 'l': plugh}]
I would like to join these lists of rows so that I can then loop through them with Jinja on an HTML page. How can I resolve this issue?

Your list comprehension should be creating a new dictionary, rather than a list of dictionaries, for each element in the result from zip():
list1 = [
{'a': 'foo', 'b': 'bar', 'c': 'baz'},
{'d': 'qux', 'e': 'quux', 'f': 'quuz'}
]
list2 = [
{'g': 'corge', 'h': 'grault', 'i': 'garply'},
{'j': 'waldo', 'k': 'fred', 'l': 'plugh'}
]
# Can also use "x | y" in place of "{**x, **y}" if on Python 3.9+
result = [{**x, **y} for x, y in zip(list1, list2)]
print(result)
This outputs:
[
{'a': 'foo', 'b': 'bar', 'c': 'baz', 'g': 'corge', 'h': 'grault', 'i': 'garply'},
{'d': 'qux', 'e': 'quux', 'f': 'quuz', 'j': 'waldo', 'k': 'fred', 'l': 'plugh'}
]

You can actually use the update function which can be used to merge two python dictionaries and is very simple and easy to use.
a = [
{"a": "foo", "b": "bar", "c": "baz"},
{"d": "qux", "e": "quux", "f": "quuz"},
]
b = [
{"g": "corge", "h": "grault", "i": "garply"},
{"j": "waldo", "k": "fred", "l": "plugh"},
]
for i in range(len(a)):
a[i].update(b[i])
print(a)
The output is as
[{'a': 'foo', 'b': 'bar', 'c': 'baz', 'g': 'corge', 'h': 'grault', 'i': 'garply'}, {'d': 'qux', 'e': 'quux', 'f': 'quuz', 'j': 'waldo', 'k': 'fred', 'l': 'plugh'}]

Your items are dictionary objects…
You can create a new dict out of the two… the keys of the last dict will overwrite the values if any are the same. The below is using the dict unpacking operator **.
final_lst = [{**d1, **d2} for d1, d2 in zip(lst1, lst2)]
There are other ways as well. Removed first example as it wasn’t correct.

Related

find difference of values between 2 array of objects in python

I have 2 array of objects:
a = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]
b = [{'a': 1, 'b': 2}, {'g': 3, 'h': 4}, {'f': 6, 'e': 5}]
Output:
a - b = [{'c': 3, 'd': 4}] ("-" symbol is only for representation, showing difference. Not mathematical minus.)
b - a = [{'g': 3, 'h': 4}]
In every array, the order of key may be different. I can try following and check for that:
for i in range(len(a)):
current_val = a[i]
for x, y in current_val.items:
//search x keyword in array b and compare it with b
but this approach doesn't feel right. Is there simpler way to do this or any utility library which can do this similar to fnc or pydash?
You can use lambda:
g = lambda a,b : [x for x in a if x not in b]
g(a,b) # a-b
[{'c': 3, 'd': 4}]
g(b,a) # b-a
[{'g': 3, 'h': 4}]
Just test if all elements are in the other array
a = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]
b = [{'a': 1, 'b': 2}, {'g': 3, 'h': 4}, {'f': 6, 'e': 5}]
def find_diff(array_a, array_b):
diff = []
for e in array_a:
if e not in array_b:
diff.append(e)
return diff
print(find_diff(a, b))
print(find_diff(b, a))
the same with list comprehension
def find_diff(array_a, array_b):
return [e for e in array_a if e not in array_b]
here is the code for subtracting list of dictionaries
a = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 6, 'f': 6}]
b = [{'a': 1, 'b': 2}, {'g': 3, 'h': 4}, {'f': 6, 'e': 6}]
a_b = []
b_a = []
for element in a:
if element not in b:
a_b.append( element )
for element in b:
if element not in a:
b_a.append( element )
print("a-b =",a_b)
print("b-a =",b_a)

How to create a nested dictionary from a single dictionary?

I am looking for a nice pythonic solution for my problem.
I have a dictionary like:
char_dist = {'b' : 0.345, 'd' : 0.158, 'c' : 0.059, 'w' : 0.437}
And I would like to get some like this:
new_dict = {'b': {'b': 0.11902,
'd': 0.05451,
'c': 0.020355,
'w': 0.150765},
'd': {'b': 0.054501,
'd': 0.024964,
'c': 0.009322,
'w': 0.150765},
'c': {'b': 0.020355,
'd': 0.009322,
'c': 0.003481,
'w': 0.025783},
'w': {'b': 0.150765,
'd': 0.069046,
'c': 0.025783,
'w': 0.190969}}
The new dict is the result of multiply the values from old dict.
new_dict = {char_dist[key] : {char_dist[key1]: char_dist[key1][value] * char_dist[key2][value], etc...
P.S. I tried some like this, but still figuring out:
new = defaultdict(dict)
for base, val in char_distribution.items():
new[base] = {base: p for base, p in
zip('bdcw', char_dist)}
pprint(matrix)
But I got the same value for all the nested dictionary:
defaultdict(<class 'dict'>,
{'b': {'b': 0.11902,
'd': 0.05451,
'c': 0.020355,
'w': 0.150765},
'c': {'b': 0.11902,
'd': 0.05451,
'c': 0.020355,
'w': 0.150765}
'd': {'b': 0.11902,
'd': 0.05451,
'c': 0.020355,
'w': 0.150765}
'w': {'b': 0.11902,
'd': 0.05451,
'c': 0.020355,
'w': 0.150765}})
I want to create a kind of transition matrix.
You can do it with nested dictionary comprehensions:
expected = {kk: {k: vv*v for k, v in char_dist.items()} for kk, vv in char_dist.items()}
print(expected)
[out]:
{'b': {'b': 0.11902, 'c': 0.02035, 'd': 0.05451, 'w': 0.15076},
'c': {'b': 0.02035, 'c': 0.00348, 'd': 0.00932, 'w': 0.02578},
'd': {'b': 0.05451, 'c': 0.00932, 'd': 0.02496, 'w': 0.06905},
'w': {'b': 0.15076, 'c': 0.02578, 'd': 0.06905, 'w': 0.19097}}
I guess if you're dealing with distributions, some linear algebra won't hurt. Meet Pandas:
import pandas as pd
....
df = pd.DataFrame([char_dist])
df.T.dot(df)
Output:
b d c w
b 0.119025 0.054510 0.020355 0.150765
d 0.054510 0.024964 0.009322 0.069046
c 0.020355 0.009322 0.003481 0.025783
w 0.150765 0.069046 0.025783 0.190969
I think the easiest:
char_dist = {'b': 0.345, 'd': 0.158, 'c': 0.059, 'w': 0.437}
old_dict = {'b': 0.68746258423, 'd': 0.5429823052, 'c': 0.5849805243, 'w': 0.95840285}
new_dict = dict.fromkeys(char_dist, old_dict)
print(new_dict)
Came up with something like this.
base_distribution = {'A' : 0.345, 'C' : 0.158, 'G' : 0.059, 'T' : 0.437}
markov = defaultdict()
for base, val in base_distribution.items():
markov[base] = markov.get(base, {})
for key, val in base_distribution.items():
p = round(base_distribution[base] * base_distribution[key], 4)
markov[base][key] = markov[base].get(key, p)
pprint(markov)
defaultdict(None,
{'A': {'A': 0.119, 'C': 0.0545, 'G': 0.0204, 'T': 0.1508},
'C': {'A': 0.0545, 'C': 0.025, 'G': 0.0093, 'T': 0.069},
'G': {'A': 0.0204, 'C': 0.0093, 'G': 0.0035, 'T': 0.0258},
'T': {'A': 0.1508, 'C': 0.069, 'G': 0.0258, 'T': 0.191}})

python list of dicts - convert each key-value as a individual dict

with a list like below that has one or more dicts
l = [{'b': 'h', 'c': (1,2)}, {'d': [0, 1], 'e': {'f': 2, 'g': 'i'} } ]
need to extract each key-value pair as an individual dict
Expected output
[{'b': 'h'}, {'c': (1,2)}, {'d': [0, 1]}, {'e': {'f': 2, 'g': 'i'} } ]
I have been trying to do this via list comprehension - the outer comprehension could be something like [ {k,v} for k, v in ?? - need some help in getting the inner comprehension.
I believe this is what you're looking for - except that the order of the elements might be different, but that's to be expected when dealing with dictionaries:
lst = [{'b': 'h', 'c': (1,2)}, {'d': [0, 1], 'e': {'f': 2, 'g': 'i'}}]
[{k: v} for d in lst for k, v in d.items()]
=> [{'c': (1, 2)}, {'b': 'h'}, {'e': {'g': 'i', 'f': 2}}, {'d': [0, 1]}]
This should work:
[{k: v} for i in l for k, v in i.items()]

Python accessing dictionary inside of dictionary returns distorted dict

I have recently been working on a python application that handles some sort of schedule. I have a dictionary that contains the number of days in a rotation in a schedule, and then each day contains a dictionary with each different part of the day. It looks like this:
schedule = {
'rotation': 6,
'1' : {'B': '8:32', 'C': '9:34', 'D' : '10:36', 'F':'12:11', 'G': '1:13', 'H':'2:15'},
'2' : {'A': '8:32', 'B': '9:34', 'C,' : '10:36', 'E':'12:11', 'F': '1:13', 'G,':'2:15'},
'3' : {'A': '8:32', 'B': '9:34', 'D,' : '10:36', 'E':'12:11', 'F': '1:13', 'H,':'2:15'},
'4' : {'A': '8:32', 'C': '9:34', 'D,' : '10:36', 'E':'12:11', 'G': '1:13', 'H,':'2:15'},
'5' : {'B' : '8:40', 'D' : '11:00', 'F' : '12:55', 'H' : '2:15' },
'6' : {'A' : '8:40', 'C' : '11:00', 'E' : '12:55', 'G' : '2:15' }
}
This all looks like it should work, yet when I print it out, I get a distorted dictionary that looks like it is sorted:
{'1': {'C': '9:34', 'B': '8:32', 'D': '10:36', 'G': '1:13', 'F': '12:11', 'H': '2:15'},
'3': {'A': '8:32', 'D,': '10:36', 'B': '9:34', 'E': '12:11', 'F': '1:13', 'H,': '2:15'},
'2': {'A': '8:32', 'B': '9:34', 'E': '12:11', 'F': '1:13', 'C,': '10:36', 'G,': '2:15'},
'5': {'H': '2:15', 'B': '8:40', 'D': '11:00', 'F': '12:55'},
'4': {'A': '8:32', 'C': '9:34', 'E': '12:11', 'G': '1:13', 'D,': '10:36', 'H,': '2:15'},
'6': {'A': '8:40', 'C': '11:00', 'E': '12:55', 'G': '2:15'},
'rotation': 6}
As you can see, in day 1, it starts with C instead of B when printing, and the 'rotation' is at the end of the dictionary instead of the front. Why does my dictionary print like this?
The order in a dictionary is not stable, due to the hash function. On top of this, Python now uses a salt value when hashing, meaning that the order will be different each run (except if you ask for a stable dict).
Python dictionary is not required to preserve order. If order is what you want then you could use lists. If you just want to view a dictionary in sorted order, you can use .sort() or sorted() to help you print.
You can sort this dictionary but you have to make an exception for rotation since its values do not fit the rest of the format of being a dictionary with alphabetical keys
d = {k: dict(sorted(v.items(), key=lambda x: x[0])) if k != 'rotation' else schedule[k] for k, v in schedule.items()}
print(d)
# {'rotation': 6, '1': {'B': '8:32', 'C': '9:34', 'D': '10:36', 'F': '12:11', 'G': '1:13', 'H': '2:15'}, '2': {'A': '8:32', 'B': '9:34', 'C,': '10:36', 'E': '12:11', 'F': '1:13', 'G,': '2:15'}, '3': {'A': '8:32', 'B': '9:34', 'D,': '10:36', 'E': '12:11', 'F': '1:13', 'H,': '2:15'}, '4': {'A': '8:32', 'C': '9:34', 'D,': '10:36', 'E': '12:11', 'G': '1:13', 'H,': '2:15'}, '5': {'B': '8:40', 'D': '11:00', 'F': '12:55', 'H': '2:15'}, '6': {'A': '8:40', 'C': '11:00', 'E': '12:55', 'G': '2:15'}}

Python How to Extract data from Nested Dict

I have output from python networkX code:
flow_value, flow_dict = nx.maximum_flow(T, 'O', 'T')
print(flow_dict)
#Output as followesenter
#{'O': {'A': 4, 'B': 6, 'C': 4}, 'A': {'B': 1, 'D': 3}, 'B': {'C': 0, 'E': 3,'D': 4}, 'C': {'E': 4}, 'E': {'D': 1, 'T': 6}, 'D': {'T': 8}, 'T': {}}
I want to extract all the data in the form looks like:
#('O','A',4),('O','B','6'),('O','C','4'),('A','B',1),......,('D','T',8)
Any ways can I traverse thru the nested dict and get the data I need?
I tried this and it works. Some type checking to only capture strings
def retrieve_all_strings_from_dict(nested_dict, keys_to_ignore = None):
values = []
if not keys_to_ignore:
keys_to_ignore = []
else: keys_to_ignore = to_list(keys_to_ignore)
if not isinstance(nested_dict,dict):
return values
dict_stack = []
dict_stack.append(nested_dict)
for dict_var in dict_stack:
data_list = [v for k,v in dict_var.items() if all([isinstance(v,str), k not in keys_to_ignore]) ]
additional_dicts = [v for k,v in dict_var.items() if isinstance(v,dict)]
for x in additional_dicts:
dict_stack.append(x)
for w in data_list:
values.append(w)
return values

Categories

Resources