summing list inside list by position in python [duplicate] - python

This question already has answers here:
Sum of list of lists; returns sum list
(10 answers)
Closed 5 years ago.
I have a list that looks like this and I would like to sum up the values at each position so as produce a list with only three values
print x
[[0, 0, -1], [0, 0, -1], [0, 0, 0], [1, 0, 0], [0, 0, 1]]
For example, x[0][1] should be summed with the value in x[1][1], x[2][1], x[3][1], x[4][1]. Likewise x[0][2] should be summed with x[1][2], x[2][2], etc.
the output should look like this
print output
[1, 0, -1]

Using numpy:
np.sum(x, axis=0)
Using native list:
[sum(y) for y in zip(*x)]

I would use Julien's answer as it is more readable but to give you an alternative, you can use map and zip functions.
list(map(sum, zip(*x)))

Just combine for loops:
outlist=[]
for i in range(len(L[0])):
sum = 0
for subL in L:
sum += subL[i]
outlist.append(sum)
print(outlist)
Output:
[1, 0, -1]

Related

change every value of a 2d array in python [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 months ago.
I have a matrix like:
matrix = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
and I have to assign a value to every element based on the formula (2 ∗ i + 3 ∗ j) mod 6, where i and j are the indexes.
I'm using the following code to iterate and assign value:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j] = (((2 * i) + (3 * j)) % 6)
but I have this output:
matrix = [
[4, 1, 4],
[4, 1, 4],
[4, 1, 4]
]
instead of the expected:
matrix = [
[0, 3, 0],
[2, 5, 2],
[4, 1, 4]
]
how can I solve this issue? Also, I can't use NumPy to solve this problem.
That is NOT how you created your array! If you did, it would work. What you did instead is
matrix = [[0]*3]*3
That gives you three references to a SINGLE list object, not three separate list objects. Initialize it as
matrix = [[0]*3 for _ in range(3)]
or better yet, use numpy.

Assigning value in python 2d list [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 12 months ago.
I have previously worked in C, I am facing problem in assigning value in 2d list
graph = [[0]*3]*3
print(graph)
graph[0][1] = 3
print(graph)
Output
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 3, 0], [0, 3, 0], [0, 3, 0]]
Expected output :
[[0, 3, 0], [0, 0, 0], [0, 0, 0]]
Is there any way to assign values other than using numpy array as answered in
Assigning values Python 2D Array
you can use a for loop to do this simply
a = []
for i in range(3):
a.append([0]*3)

How to fill up a matrix in Python using for loops

How to add zeroes to the end of a list and fill a matrix with it?
Currently I have
(1,0,1,1,0)
How to fill up a matrix such that it looks like this:
[[0, 0, 0],
[0, 1, 0],
[1, 1, 0]]
In your question, you have clearly added the zeroes to the beginning of the matrix rather than the end but whatever.
To extend a list to one with 9 items with preceeding zeroes:
list_out = [0]*(9-len(list_in)) + list_in
to extend a list to one with 9 items with trailing zeroes just reverse the order:
list_out = list_in + [0]*(9-len(list_in))
We can convert a list with 9 items to a matrix using
matrix = [li[0:3,li[3:6],li[6:9]
so eg.
list_in = [1,2,3]
li = list_in + [0]*(9-len(list_in))
matrix = [li[0:3],li[3:6],li[6:9]]
gives
[[1, 2, 3], [0, 0, 0], [0, 0, 0]]

Update value in Multidimensional list in Python [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 5 years ago.
I have an list like the following
line_37_data = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
When I print line_37_data[0][0] , the value 0 is printed.
When I update the list as line_37_data[0][0] = 5, the list gets modified like below
[[5, 0, 0], [5, 0, 0], [5, 0, 0]]
How can I can update the value in the list based on the index ?
Note :- I don't use NumPy. This is pure plain Python without any libraries. I am using 2.7 and not Python 3
If you pass in the same list as each element of your outer list, manipulating it will show in each place it appears. If you're just looking to fill a 2d list with zeros, list comprehension would be easy:
def generate_2d(h, w):
return [[0 for x in range(w)] for y in range(h)]
array = generate_2d(3, 3)
# Format is array[y][x] based on names in function
array[0][0] = 5
array[1][2] = 7
assert array == [
[5, 0, 0],
[0, 0, 7],
[0, 0, 0]]

Automating the creation of a list of lists - Python

I have to create a list of lists that is summarized below:
list_of_lists = [[0,1],[0,2],[0,3]....[0,N]]
Basically just need the first element of each sub-list to be 0, and the second element to be 1 more than the prior value of the second element.
The value for N is about 2000, so obviously I do not want to type out the whole thing. Is there a simple way to automate with Python?
Thank You
You can use simple list comprehension with range:
>>> N = 5
>>> [[0, i] for i in range(1, N + 1)]
[[0, 1], [0, 2], [0, 3], [0, 4], [0, 5]]

Categories