a = [1, 2]
b = [[5,6], [7,8]]
c = list(zip(a, b))
print("c zipped:", c)
i = 0
lenghta = len(a)
c = []
while i < lengtha:
temp_list = [a[i], b[i]]
c.append(temp_list)
i += 1
print("c: ", c)
output:
c zipped: [(1, [5, 6]), (2, [7, 8])] c: [[1, [5, 6]], [2, [7, 8]]]
What I am expecting is:
[[1, 5, 6], [2, 7, 8]]
This seems overcomplicated. Try this, using a list comprehension:
a = [1, 2]
b = [[5,6], [7,8]]
c = [[x] + b[i] for i, x in enumerate(a)]
I know this isn't using zip(), but you could do:
c = []
for i in range(len(a)):
c.append([a[i], b[i]])
Using zip and list comprehension
a = [1, 2]
b = [[5,6], [7,8]]
[[i]+j for i,j in zip(a,b)]
#[[1, 5, 6], [2, 7, 8]]
you can also use itertools.chain
>>> from itertools import chain
>>> a = [1, 2]
>>> b = [[5,6], [7,8]]
>>> c = [list(chain([x], y)) for (x, y) in zip(a, b)]
>>> c
[[1, 5, 6], [2, 7, 8]]
Related
Appreciate your help, this is a question about list of lists.
Given lists:\
g = [[] for _ in range(5)]
a = [1,2,3,4]
b = [37100,3710,371,37,0]
I need the result to be g = [[37100,1],[3710,2],[371,3],[37,4],[0]], I tried to use nested for loops, but that is not working, below is my code:
g = [[] for _ in range(5)]
a=[1,2,3,4]
b = [37100,3710,371,37,0]
for i in range(len(b)):
g[i].append(b[i])
for i in g[0:5]:
for b in range(len(a)):
lst = []
lst.append(a[b])
i.append(lst)
print(g)
However, my result is [[37100, [4]], [3710, [4]], [371, [4]], [37, [4]], [0, [4]]]
Anyone knows how to solve it?
You can use itertools.zip_longest
from itertools import zip_longest
a = [1,2,3,4]
b = [37100,3710,371,37,0]
c = [1,2,3,4,5,6]
d = [1,2,3,4,5,6,7]
res = [[ii for ii in i if ii is not None] for i in zip_longest(b, a, c)]
print(res)
res = [[ii for ii in i if ii is not None] for i in zip_longest(b, a, c, d)]
print(res)
Output
[[37100, 1, 1], [3710, 2, 2], [371, 3, 3], [37, 4, 4], [0, 5], [6]]
[[37100, 1, 1, 1], [3710, 2, 2, 2], [371, 3, 3, 3], [37, 4, 4, 4], [0, 5, 5], [6, 6], [7]]
Edit based on comment:
from itertools import repeat
a = [1,2,3,4]
b = [371] * 100
c = [11,12,13,14]
d = [111,222,333,444]
res = [list(j) for i in zip(repeat(b), (a, c, d)) for j in zip(*i)]
print(res)
Here a solution that isn't data-dependant
a = [1, 2, 3, 4]
b = [37100, 3710, 371, 37, 0]
g = [[] for _ in range(max(len(a), len(b)))]
for i, sublist in enumerate(g):
sublist.extend(b[i:i + 1])
sublist.extend(a[i:i + 1])
Or with g unprepared
a = [1, 2, 3, 4]
b = [37100, 3710, 371, 37, 0]
g = []
for i in range(max(len(a), len(b))):
g.append(b[i:i + 1] + a[i:i + 1])
Or itertools.zip_longest allows to pair data, and fill with a default value when one iterable is shorter, then you have to filter out that default value (None by default)
a = [1, 2, 3, 4]
b = [37100, 3710, 371, 37, 0]
g = [[val for val in pair if val is not None]
for pair in zip_longest(b, a)]
# Assuming len(a) <= len(b)
g = []
for i in range(len(a)) :
g.append([b[i], a[i]])
for i in range(len(a), len(b)) :
g.append([b[i]])
Using map and lambda
a = [1,2,3,4]
b = [37100,3710,371,37,0]
map(lambda x, y: [x, y] if x is not None else [y], a, b)
Output
[[37100, 1], [3710, 2], [371, 3], [37, 4], [0]]
In the case of lengh a and b not equal
map(lambda x, y: [x, y] if x is not None and y is not None else ([y] if y is not None else [x]), a, b)
the expected output can be obtained by:
[list(x) for x in zip(a,b)] + [b[len(a):]]
It clearly depends on your data, and the general purpose of your code.
to read more about zip, see the official docs: https://docs.python.org/3/library/functions.html#zip
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 am trying to get the elements as a list if any of them in the same position in two 2D matrices.
Let's consider matrix A and matrix B
A = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
B = [[0, 2, 5],[6, 2, 4],[7, 8, 1]]
The resultant list should be
C = [2, 7, 8]
I have tried using a filter but it is not working.
def find(A):
for i in range(3):
for j in range(3):
if A[i][j] == B[i][j]:
return A[i][j]
else:
return False
A = [[1, 2, 3],[4, 5, 6], [7, 8, 9]]
B = [[0, 2, 5],[6, 2, 4], [7, 8, 1]]
C = filter(find, A)
for element in C:
print(element)
Note: Please use a filter to achieve the target.
Solution using filter:
def find(L):
A, B = L
if A == B:
return True
else:
return False
A = [[1, 2, 3],[4, 5, 6], [7, 8, 9]]
B = [[0, 2, 5],[6, 2, 4], [7, 8, 1]]
# flatten lists
A = [item for row in A for item in row]
B = [item for row in B for item in row]
C = filter(find, zip(A,B))
# filter returns a tuple for each row but we only want the single element
C = [item[0] for item in C]
print(list(C))
Gives us
[2, 7, 8]
Try this,
def func(t):
return True if t[0] == t[1] else False
A = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
B = [[0, 2, 5],[6, 2, 4],[7, 8, 1]]
C = [item1 for lst1, lst2 in zip(A, B) for item1, item2 in filter(func, zip(lst1, lst2))]
>>> C
[2, 7, 8]
Basic Answer
First we loop over A and B, i and z now holds the inner lists.
Then we loop over those inner lists using g and q, if they are equal, we append them to the empty list.
to_fill = []
for i,z in zip(A,B):
for g,q in zip(i,z):
if g==q:
to_fill.append(g)
Adapt it as you please.
The most efficient way of achieving what you want is by using numpy:
import numpy as np
A = np.asarray(A)
B = np.asarray(B)
C = A[A==B]
I have a list:
lst = [[7], [4, 3, 5, 8], [1, 3]]
How can I multiply each element in list by it position like this:
[[7 * 0],[4 * 0 + 3 * 1 + 5 * 2 + 8 * 3], [1 * 0 + 3 * 1]]
And print answer:
answer = [[0], [37], [3]]
You can use a list comprehension with sum and enumerate:
L = [[7], [4, 3, 5, 8], [1, 3]]
res = [[sum(i*j for i, j in enumerate(sublist))] for sublist in L]
print(res)
[[0], [37], [3]]
Or if you are happy to use a 3rd party library, you can use NumPy:
import numpy as np
L = [[7], [4, 3, 5, 8], [1, 3]]
res = [np.arange(len(sublist)).dot(sublist) for sublist in L]
print(res)
[0, 37, 3]
This is a possible solution ...
a_list = [[7], [4, 3, 5, 8], [1, 3]]
new_list = []
for sub_list in a_list:
sublistsum = 0
for i, value in enumerate(sub_list):
sublistsum = sublistsum + i * value
new_list.append([sublistsum])
print(new_list)
Suppose I have a python list l=[1,2,3,4,5]. I would like to find all x-element lists starting with elements that satisfy a function f(e), or the sublist going to the end of l if there aren't enough items. For instance, suppose f(e) is e%2==0, and x=3 I'd like to get [[2,3,4],[4,5]].
Is there an elegant or "pythonic" way to do this?
>>> f = lambda e: e % 2 == 0
>>> x = 3
>>> l = [1, 2, 3, 4, 5]
>>> def makeSublists(lst, length, f):
for i in range(len(lst)):
if f(lst[i]):
yield lst[i:i+length]
>>> list(makeSublists(l, x, f))
[[2, 3, 4], [4, 5]]
>>> list(makeSublists(list(range(10)), 5, f))
[[0, 1, 2, 3, 4], [2, 3, 4, 5, 6], [4, 5, 6, 7, 8], [6, 7, 8, 9], [8, 9]]
Using a list comprehension:
>>> l = range(1,6)
>>> x = 3
>>> def f(e):
return e%2 == 0
>>> [l[i:i+x] for i, j in enumerate(l) if f(j)]
[[2, 3, 4], [4, 5]]