Reputation: 35
I have a bit of strange problem with Python’s matrix making capabilities.
I have the following lists:
Test01 = [11,25,53,19]
Test02 = [2,6,4,8]
I want to write a code that adds each element in each list like so:
11 + 2, 11 + 6, 11 + 4, 11+ 8
25 + 2, 25 + 6
Etc. etc.
The following code I wrote
for i in test01:
c = [i+x for x in test02]
print(c)
This is the result I get out of it (and what I wanted).
[13,17,15,19]
[27,31,29,33]
[55,59,57,61]
[21,25,23,27]
Now I have a large dataset and made some calculations. The first two rows I selected to make the same calculation (i.e. the same type of matrix). However, Python only adds like so:
New_list01 = 1, 2, 3, 4
New_list02 = a,b,c,d
[[1+a, 2+ b, 3+ c, 4+ d]]
Only one row instead of the expected four Why is this? I converted the lists into numpy (.to_numpy) and to to list (.tolist()) as well. Still the same (wrong) answer. How can I still make the calculation I want?
Thanks for the help! (BTW, if there is a built-in function that can perform the task I am looking for then that would be even better.)
Upvotes: 3
Views: 81
Reputation: 3594
You can simply do this with list comprehension in one line. Not sure what output you exactly wanted, therefore adding couple of options:
>>> New_list01 = [1, 2, 3, 4]
>>> New_list02 = ['a','b','c','d']
>>> out = [[i+str(j) for i in New_list02] for j in New_list01]
>>> print(out)
[['a1', 'b1', 'c1', 'd1'],
['a2', 'b2', 'c2', 'd2'],
['a3', 'b3', 'c3', 'd3'],
['a4', 'b4', 'c4', 'd4']]
>>> out = [[i+str(j) for i in New_list02] for j in New_list01]
>>> print(out)
[['a1', 'b1', 'c1', 'd1'],
['a2', 'b2', 'c2', 'd2'],
['a3', 'b3', 'c3', 'd3'],
['a4', 'b4', 'c4', 'd4']]
>>> out = [[str(j) + i for i in New_list02] for j in New_list01]
[['1a', '1b', '1c', '1d'],
['2a', '2b', '2c', '2d'],
['3a', '3b', '3c', '3d'],
['4a', '4b', '4c', '4d']]
>>> out = [[str(j) + '+' + i for i in New_list02] for j in New_list01]
[['1+a', '1+b', '1+c', '1+d'],
['2+a', '2+b', '2+c', '2+d'],
['3+a', '3+b', '3+c', '3+d'],
['4+a', '4+b', '4+c', '4+d']]
>>> out = [[i + '+' + str(j) for i in New_list02] for j in New_list01]
[['a+1', 'b+1', 'c+1', 'd+1'],
['a+2', 'b+2', 'c+2', 'd+2'],
['a+3', 'b+3', 'c+3', 'd+3'],
['a+4', 'b+4', 'c+4', 'd+4']]
>>> # Filtering out one row
>>> out[0]
['a+1', 'b+1', 'c+1', 'd+1']
If you want a flattened
(one list) output:
>>> just remove inner []
>>> out = [i+ '+' + str(j) for i in New_list02 for j in New_list01]
>>> print(out)
['a+1', 'a+2', 'a+3', 'a+4', 'b+1', 'b+2', 'b+3', 'b+4', 'c+1', 'c+2', 'c+3', 'c+4', 'd+1', 'd+2', 'd+3', 'd+4']
>>> out = [i + str(j) for i in New_list02 for j in New_list01]
>>> print(out)
['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4', 'c1', 'c2', 'c3', 'c4', 'd1', 'd2', 'd3', 'd4']
For your initial test case :
>>> out = [[i+j for i in Test02] for j in Test01]
>>> print(out)
[[13, 17, 15, 19],
[27, 31, 29, 33],
[55, 59, 57, 61],
[21, 25, 23, 27]]
>>> #first row
>>> print(out[0])
[13, 17, 15, 19]
>>> out = [[i+j for i in Test02] for j in Test01]
[13, 27, 55, 21, 17, 31, 59, 25, 15, 29, 57, 23, 19, 33, 61, 27]
Upvotes: 1
Reputation: 35
Thank you so much for responding. I asked a colleague and he helped me solving this as follows:
#The list of lists
list_of_lists = [range(4), range(7)]
flattened_list = []
#flatten the list
for x in list_of_lists:
for y in x:
flattened_list.append(y)
The issue was that the list had lists and needed to be flattened
.
Upvotes: 0
Reputation: 76
I think there is a problem with how you use your function as it works as expected
You can use Numpy then use a loop and pass your values to the second matrix
import numpy as numpy
matrix1 = numpy.array([2, -7, 5])
matrix2 = numpy.array([5, 8, -5])
total = numpy.add(matrix1, matrix2)
print ("output added array : ", total)
Output:
output added array : [7 1 0]
You can also use it for multidimensional array
import numpy as numpy
martix1 = numpy.array([[2, 2, 2], [-7, -7, -7], [5, 5, 5]])
matrix2 = numpy.array([[5, 8, -5], [5, 8, 5], [5, 8, -5]])
total = numpy.add(martix1, matrix2)
print("output added array : \n", total)
Output:
output added array :
[[ 7 10 -3]
[-2 1 -2]
[10 13 0]]
Upvotes: 0