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]
Related
l = [ [[1,2,3], [2,3,4]] ]
sum(l,[])
Output ist
[[1, 2, 3], [2, 3, 4]]
I don't know why it works like this.
I tired the two below
[].__add__(l) # First Try
[] + l # Secodn Try
In both cases, the output was as follows.
[[[1, 2, 3], [2, 3, 4]]]
I don't know what the difference is from the sum operation.
My python version is 3.8.12
sum takes a list of values and adds all of them together. E.g. for a list of numbers:
l = [1, 2, 3]
sum(l)
does:
0 + 1 + 2 + 3
As you see, the outer list does not appear in the operations nor result.
Now, with your example:
l = [ [[1,2,3], [2,3,4]] ]
sum(l, [])
that's concatenating all the values in l:
[] + [[1,2,3], [2,3,4]]
Which is not the same as:
[] + [ [[1,2,3], [2,3,4]] ]
If I understand correctly you want to add 1 to every item in the list, if so this code should work.
x = [[[1,2,3], [2,3,4]]]
for i in range(len(x[0])):
for j in range(len(x[0][i])):
x[0][i][j] += 1
print(x)
I'd like to remove same lists in a list having len(a) = 5 and a = [[1,2,3],[2,3,4], [0,1,2],[2,4,6],[3,6,9]] as results.
How can I get that?
a1 = [[1,2,3],[2,3,4]]
a2 = [[0,1,2],[2,4,6]]
a3=[[1,2,3],[0,1,2],[3,6,9]]
a = a1+a2+a3
a = [tuple(l) for l in a]
print(set(a))
print(len(a))
a=[list(ele) for ele in a]
print(a)
print(len(a))
You cannot make a set of lists of lists because they are not hashable. You could first convert them to tuples and then create a set:
a1 = [[1,2,3],[2,3,4]]
a2 = [[0,1,2],[2,4,6]]
a3=[[1,2,3],[0,1,2],[3,6,9]]
a = a1+a2+a3
a = [list(x) for x in set([tuple(L) for L in a])]
output:
[[0, 1, 2], [2, 4, 6], [1, 2, 3], [2, 3, 4], [3, 6, 9]]
I want to swap every occurrence of two elements in a list of lists. I have seen previous answers but they are different from what I'm trying to do. Also, I am looking for a clean and concise way (preferably list-comprehension) for this purpose.
Input
my_list = [[1,2], [1,3], [1,4], [3,2]]
Output
my_list = [[2,1], [2,3], [2,4], [3,1]]
I am trying to do something like this but no success:
[1 if i==2 else i, 2 if i==1 else i for i in my_list]
Here's a simple solution using list comprehension:
my_list = [[1,2], [1,3], [1,4], [3,2]]
a = 1
b = 2
my_list = [[a if i==b else b if i==a else i for i in j] for j in my_list]
print(my_list) # [[2, 1], [2, 3], [2, 4], [3, 1]]
If you want to add more elements to replace you can use a dictionary:
swap = {
1: 2,
2: 1
}
my_list = [[swap.get(i, i) for i in j] for j in my_list]
Someone else will probably answer with something better, but this would work.
def check(num):
if num == 1:
return 2
elif num == 2:
return 1
else:
return num
out = [[check(j) for j in i] for i in my_list]
If you need to swap 1's with 2's, this one-liner will work:
>>> my_list = [[1,2], [1,3], [1,4], [3,2]]
>>> print([[(lambda k: (1 if val == 2 else 2) if val in [1, 2] else val)(val) for val in sub_list] for sub_list in my_list])
[[2, 1], [2, 3], [2, 4], [3, 1]]
read the second line from right to left... in chunks!
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]])
I have two lists of equal length.
The first list consists of 1000 sublists with two elements each e.g
listone = [[1,2], [1,3], [2,3],...]
My second list consists of 1000 elements e.g.
secondlist = [1,2,3,...]
I want to use these two lists to make my third list consisting 1000 sublists of three elements. I want to have my list in such a fashion that every index is added from secondlist to listone as the third element e.g.
thirdlist = [[1,2,1], [1,3,2], [2,3,3],...]
Look into zip:
listone = [[1,2], [1,3], [2,3]]
secondlist = [1,2,3]
thirdlist = [x + [y] for x, y in zip(listone, secondlist)]
print(thirdlist)
# Output:
# [[1, 2, 1], [1, 3, 2], [2, 3, 3]]
you can do:
[x + [y] for x, y in zip(listone, secondlist)]
You can use the zip function to combine listOne and second. To get the desired format you have to create a new list using list comprehension
listOne = [[1,2], [1,3], [2,3]]
secondList = [1,2,3]
thirdList = [x + [y] for x,y in zip(listOne, secondList)]
print thirdList
>>> [[1, 2, 1], [1, 3, 2], [2, 3, 3]]