concatenating arrays stored in different dictionaries - python-2.7

I have large number of dictionaries with about 20 keys in each but using two dictionaries with only 2 keys as example here:
dict1 = {'A':np.array([[1,2,3],[4,5,6]]), 'B':np.array([[1,2],[4,5]])}
dict2 = {'A':np.array([[11,12,13],[14,15,16]]), 'B':np.array([[11,21],[41,51]])}
I am trying to obtain new dictionary with concatenated arrays such that:
combinedDict['A'] =
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 12, 13],
[14, 15, 16]])
combinedDict['B'] =
array([[ 1, 2],
[ 4, 5],
[11, 21],
[41, 51]])
How do I write a dictionary comprehension or other approach for the above?

using numpy.concatenate
dictkeys = ('A', 'B')
dicts = dict1, dict2
{key: np.concatenate([d[key] for d in dicts]) for key in dictkeys}

Related

How to create a new dictionary, that has the values of one dictionary as keys and the values of another dictionary as values

I have two dictionaries. In reality they are much longer.
First:
Data = { "AT" : [1,2,3,4], "BE" : [ 4,2,1,6], "DE" : [ 5,7,8,9] }
2nd dict:
Data2 = { "AT" : ["AT1","AT2","AT3"], "BE" : ["BE1","BE2","BE3"], "DE" : ["DE1","DE2","DE3","DE4","DE5"] }
I now want a dictionary that assigns the values of the first to the values
of the 2nd dictionary (the values of the 2nd dictionary are the new keys.)
Something like:
Result = { "AT1" : [1,2,3,4], "AT2" : [1,2,3,4], "AT3" : [1,2,3,4], "BE1" : [4,2,1,6], "BE2" : [4,2,1,6] and so on... }
I tried:
search= {k : v for k, v in Data.itervalues()}
result = {search[v]: k for v in Data2.itervalues() for k in Data.itervalues()}
print(result)
I get a too many values to unpack error and despite that im not quite sure if this does what i want it to do :/
Error:
Traceback (most recent call last):
File "<ipython-input-18-e4ca3008cc18>", line 17, in <module>
search= {k : v for k, v in Data.itervalues()}
File "<ipython-input-18-e4ca3008cc18>", line 17, in <dictcomp>
search= {k : v for k, v in Data.itervalues()}
ValueError: too many values to unpack
Thanks for any help in advance.
Here is a way to do it using a dictionary comprehension :
result = {y : Data[x] for x in Data2 for y in Data2[x]}
Output :
{'AT1': [1, 2, 3, 4],
'AT2': [1, 2, 3, 4],
'AT3': [1, 2, 3, 4],
'BE1': [4, 2, 1, 6],
'BE2': [4, 2, 1, 6],
'BE3': [4, 2, 1, 6],
'DE1': [5, 7, 8, 9],
'DE2': [5, 7, 8, 9],
'DE3': [5, 7, 8, 9],
'DE4': [5, 7, 8, 9],
'DE5': [5, 7, 8, 9]}
Way without using comprehension.
result = {}
for k,v in Data2.items():
for _v in v:
result[_v]=Data[k]
print(result)

Create a function genSet() that takes in a list of numbers and returns a sorted set in python

I am new to python and trying to solve this example on pyschool
I need to write a function,
a) that takes a list of numbers
b) removes duplicates from the list
c) returns a sorted set:
In python, example :
>>> genSet([5,4,8,4,9,8])
[4, 5, 8, 9 ]
>>> genSet([3,-2,-1,-1,3,-2,0])
[-2, -1, 0, 3 ]
Removing Duplicates:
>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> list(set(t))
[1, 2, 3, 5, 6, 7, 8]
>>> s = [1, 2, 3]
>>> list(set(t) - set(s))
[8, 5, 6, 7]
Sort the List:
>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
Now you could write your custom function, that removes duplicates and sort.
The following code snippet works:
def genSet(clist):
t = list(set(clist))
return sorted(t)
if __name__ == "__main__":
print genSet([5,4,8,4,9,8])
print genSet([3,-2,-1,-1,3,-2,0])
If you want to iterate over multiple list try this:
a = [
[5,4,8,4,9,8],
[3,-2,-1,-1,3,-2,0]
]
for aa in a:
print genSet(aa)
set will automatically remove duplicates and sorted will sort the list.
def genSet(l):
return (sorted(set(l)))

Extracting from a Nested list Using Indexing and slicing

here is my current list: [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
want to extract from the list and nested items using indexing and slicing, this is what I want to extract: [0, 2, 3, [5 ,6], 8, 10]
code so far:
list = [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
new_list = list[0], list[2], list[3], list[4]
print("new list is", new_list)
outputs this: new list is (0, [1, 2, 3, 4], [[5], [6, 7]], [8, 9, 10]), need to extrac tthe nexted items and format the list like this: [0, 2, 3, [5 ,6], 8, 10]
L = [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
new_list = [L[0], L[2][1], L[2][2], [L[3][0][0], L[3][1][0]], L[-1][-3], L[-1][-1]]
print("new list is", new_list)
print()

Why the map function doesn't return the list without duplicate elements?

I have this list:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
And I wat to get rid of duplicate values. The code for the map function is taken from here.
this is the complete testing code:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
list2 = []
map(lambda x: not x in list2 and list2.append(x), list1)
print(list2)
list2 = []
[list2.append(c) for c in list1 if c not in list2]
print(list2)
list2 = []
for c in list1:
if c not in list2:
list2.append(c)
print(list2)
In Python 2.7 is prints:
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
In Python 3.4 it prints:
[]
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
Why the map function returns an empty list in Python3?
Because in python-3.x a map is not evaluate immediately. It works as a generator where elements are generated on the fly by need: this can be more efficient since it is possible you for instance only need the first three elements so why would you calculate all elements? So as long as you do not materialize the output of map in a way, you have not really calculated the map.
You can for instance use list(..) to force Python to evaluate the list:
list(map(lambda x: not x in list2 and list2.append(x), list1))
In that case python-3.x will generate the same result for list2.

Python 2.x: Summing items in a list of tuples

For summation purposes, I created a list of tuples, where multiple items in the list have the same first variable.
for example:
x = [('m32',[1,2,3]),('m32',[2,3,4]),('m32',[4,5,6]),('m33',[1,2,3]),('m33',[2,3,4]),('m33',[4,5,6]),('m34',[1,2,3]),('m34',[2,3,4]),('m34',[4,5,6])....]
I want to add the individual values of the second items in the tuples (i.e 1+2+4, 2+3+5, 3+4+6) for all values that have the same first item (i.e. 'm32').
In other words, for all items labeled 'm32', I want to be able to add the other values.
How can I slice/index this to loop through and perform the summation?
Some tricky zip magic, along with itertools.groupby to group the matching first items together:
>>> x = [('m32',[1,2,3]),('m32',[2,3,4]),('m32',[4,5,6]),('m33',[1,2,3]),('m33',[2,3,4]),('m33',[4,5,6]),('m34',[1,2,3]),('m34',[2,3,4]),('m34',[4,5,6])]
>>> from itertools import groupby
>>> from operator import itemgetter
>>> for k,g in groupby(x,key=itemgetter(0)):
... print (k,[sum(i) for i in zip(*zip(*g)[1])])
...
('m32', [7, 10, 13])
('m33', [7, 10, 13])
('m34', [7, 10, 13])
A breakdown of how it works:
g is the group of items with matching keys. zip(*g) transposes the matrix, bringing the keys and values together:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print zip(*g)
...
[('m32', 'm32', 'm32'), ([1, 2, 3], [2, 3, 4], [4, 5, 6])]
[('m33', 'm33', 'm33'), ([1, 2, 3], [2, 3, 4], [4, 5, 6])]
[('m34', 'm34', 'm34'), ([1, 2, 3], [2, 3, 4], [4, 5, 6])]
Getting the 2nd items:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print zip(*g)[1]
...
([1, 2, 3], [2, 3, 4], [4, 5, 6])
([1, 2, 3], [2, 3, 4], [4, 5, 6])
([1, 2, 3], [2, 3, 4], [4, 5, 6])
Transposing again to match up the items to sum:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print zip(*zip(*g)[1])
...
[(1, 2, 4), (2, 3, 5), (3, 4, 6)]
[(1, 2, 4), (2, 3, 5), (3, 4, 6)]
[(1, 2, 4), (2, 3, 5), (3, 4, 6)]
And adding them up:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print [sum(i) for i in zip(*zip(*g)[1])]
...
[7, 10, 13]
[7, 10, 13]
[7, 10, 13]
The answer given by Mark is great, and probably much more efficient that the one I'll post you. But I still want to post my answer because you are probably new to python and it will be easy for you to understand it.
For this kind of scripts you only need some imagination and basic python notions:
dictionary={}
for name, numbers in x:
if name in dictionary:
current_list=dictionary[name]
for i in range(3):
current_list[i]+=numbers[i]
else:
dictionary[name]=numbers
print(dictionary)
Note that the output is a dictionary:
{'m32': [7, 10, 13], 'm33': [7, 10, 13]}..
I hope it help you!