Let's say I have three arrays like this:
Total = [], A = [1,2,3] and B = [4,5,6].
When I use Total = np.append(Total,A) it gives me: [1,2,3]
And when I use Total = np.append(Total,B) it gives me: [1,2,3,4,5,6]
What I want after the second append is: Total = [[1,2,3],[4,5,6]].
Why not just stack them?
A = [1,2,3]
B = [4,5,6]
Total = np.vstack((A,B))
print(Total)
OUTPUT:
[[1 2 3]
[4 5 6]]
OR
just add the lists:
T = [A] + [B]
print(T)
OUTPUT:
[[1, 2, 3], [4, 5, 6]]
You can do this a few ways:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.vstack([a, b])
np.stack([a, b], axis=0)
np.c_[a, b].T
np.concatenate([a[np.newaxis], b[np.newaxis]])
Related
My question is as follows:
I have some lists such as [1], [1,2], [1,3,1], and I want to put them in a 2D array result, which is [[1],[1,2],[1,3,1]]. Are there any simple ways to do that? Thanks.
yea, there are
l1, l2, l3=[1], [1,2], [1,3,1]
biglist=[l1,l2,l3]
Try this:
l1 = [1]
l2 = [1, 2]
l3 = [1, 3, 1]
result = []
result.append(l1)
result.append(l2)
result.append(l3)
print(result)
Or you can write shorter:
l1 = [1]
l2 = [1, 2]
l3 = [1, 3, 1]
result = [l1,l2,l3]
print(result)
output:
[[1], [1, 2], [1, 3, 1]]
While I don't think there is something exactly the same as a 2D Array, you can just put those lists in a 2D list (as SuperStew shows):
a = [1]
b = [1,2]
c = [1,3,1]
d = [a,b,c]
Now, you can call d like you would a 2D Array. For Example:
print d[0][0]
print d[2][1]
Returns:
1
3
There you go:
x, y, z = [1], [1,2], [1,3,1]
[x,y,z]
Why this slicing example doesn't give the same results as standard lists? It works like if it first evaluates an[:2] = bn[:2] and then bn[:2] = an[:2].
import numpy as np
l1 = [1, 2, 3]
l2 = [4, 5, 6]
a = list(l1)
b = list(l2)
an = np.array(a)
bn = np.array(b)
print(a, b)
a[:2], b[:2] = b[:2], a[:2]
print(a, b)
print(an, bn)
an[:2], bn[:2] = bn[:2], an[:2]
print(an, bn)
Output:
--------------------
[1, 2, 3] [4, 5, 6]
[4, 5, 3] [1, 2, 6]
--------------------
[1 2 3] [4 5 6]
[4 5 3] [4 5 6]
--------------------
If I do it like this - everything works:
dummy = an[:2]
an[:2] = bn[:2]
bn[:2] = dummy
For lists a[:2] is a copy of the list with the first two elements, for numpy arrays this is only a reference.
You need to make a copy, explicitly:
an[:2], bn[:2] = bn[:2].copy(), an[:2].copy()
In Python, is there a good way to add/sum (or otherwise combine) two lists of uneven length?
e.g. given some lists:
a = [1,2,3]
b = [1,2,3,4]
produce list c:
c = [2,4,6,4]
where each element is the sum of a and b, taking a missing element as zero?
Yes, you can use itertools.zip_longest():
>>> from itertools import zip_longest
>>> a = [1, 2, 3]
>>> b = [1, 2, 3, 4]
>>> [x + y for x, y in zip_longest(a, b, fillvalue=0)]
[2, 4, 6, 4]
Here's what I landed up using:
[ (ax or 0) + (bx or 0) for (ax, bx) in map(None, a, b) ]
where n or 0 is used to coalesce None to zero, and map(None, a, b) is used as a null-expanding version of zip.
Problems? Better answers?
Another option:
In [1]: a = [1, 2, 3]
In [2]: b = [1, 2, 3, 4]
In [3]: [i+ii if i and ii else i or ii for (i,ii) in map(lambda x,y: (x,y),a,b)]
Out[3]: [2, 4, 6, 4]
I have two lists:
A = [2, 4, 6, 8, 10]
B = [1, 3, 5, 7, 9]
How do I subtract each value in one list from the corresponding value in the other list and create a list such that:
C = [1, 1, 1, 1, 1]
Thanks.
The easiest way is to use a list comprehension
C = [a - b for a, b in zip(A, B)]
or map():
from operator import sub
C = map(sub, A, B)
Since you appear to be an engineering student, you'll probably want to get familiar with numpy. If you've got it installed, you can do
>>> import numpy as np
>>> a = np.array([2,4,6,8])
>>> b = np.array([1,3,5,7])
>>> c = a-b
>>> print c
[1 1 1 1]
Perhaps this could be usefull.
C = []
for i in range(len(A)):
difference = A[i] - B[i]
C.append(difference)
One liner:
A = [2, 4, 6, 8, 10]
B = [1, 3, 5, 7, 9]
[A[x]-B[x] for x in range(len(B))]
#output
[1, 1, 1, 1, 1]
Here I go with my basic questions again, but please bear with me.
In Matlab, is fairly simple to add a number to elements in a list:
a = [1,1,1,1,1]
b = a + 1
b then is [2,2,2,2,2]
In python this doesn't seem to work, at least on a list.
Is there a simple fast way to add up a single number to the entire list.
Thanks
if you want to operate with list of numbers it is better to use NumPy arrays:
import numpy
a = [1, 1, 1 ,1, 1]
ar = numpy.array(a)
print ar + 2
gives
[3, 3, 3, 3, 3]
using List Comprehension:
>>> L = [1]*5
>>> [x+1 for x in L]
[2, 2, 2, 2, 2]
>>>
which roughly translates to using a for loop:
>>> newL = []
>>> for x in L:
... newL+=[x+1]
...
>>> newL
[2, 2, 2, 2, 2]
or using map:
>>> map(lambda x:x+1, L)
[2, 2, 2, 2, 2]
>>>
You can also use map:
a = [1, 1, 1, 1, 1]
b = 1
list(map(lambda x: x + b, a))
It gives:
[2, 2, 2, 2, 2]
try this. (I modified the example on the purpose of making it non trivial)
import operator
import numpy as np
n=10
a = list(range(n))
a1 = [1]*len(a)
an = np.array(a)
operator.add is almost more than two times faster
%timeit map(operator.add, a, a1)
than adding with numpy
%timeit an+1
If you don't want list comprehensions:
a = [1,1,1,1,1]
b = []
for i in a:
b.append(i+1)