Related
a = [[1,3,45,6,78,9],[2,6,5,88,3,4],[44,66,2,4,77,12]]
b = [4,6,3]
These are two lists in python, now each element in the first list a corresponds to the element in the list b with the same index. i.e a[0]:4,a[1]:6,a[2]:3 and so on.
Now I want to sort list b and then print the respective values corresponding to list a.
I cannot use a dictionary because it gives an error that a is not hashable. My desired output is:
x = [[44,66,2,4,77,12], [1,3,45,6,78,9], [2,6,5,88,3,4]]
You can do :
a = [[1,3,45,6,78,9],[2,6,5,88,3,4],[44,66,2,4,77,12]]
b = [4,6,3]
c = sorted([[b[i],a[i]] for i in range(len(a))])
x = [i[1] for i in c]
It can be done on a single line like:
a = [[1,3,45,6,78,9],[2,6,5,88,3,4],[44,66,2,4,77,12]]
b = [4,6,3]
c = [el for _, el in sorted(zip(b, a))]
print(c)
Output:
[[44, 66, 2, 4, 77, 12], [1, 3, 45, 6, 78, 9], [2, 6, 5, 88, 3, 4]]
(If the values in b are not unique, it will sort by first element in the corresponding the values from a)
There is no issue using a dictionary as long as you avoid using any list as a key. In this case, you could use b for keys and a for values. For example,
d = dict(zip(b,a))
print([d[k] for k in sorted(d)])
I would like to return the duplicates in two different 2d lists. But I'm having trouble figuring out what code to write. For example I would like variable "a" to compare to variable "b" and return the duplicates. Here are my two 2d list below.
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
I would like my results to be:
c = [[2,3,6,8],[15,17,21,22]]
You just need to check if a list in a is also in b or not.
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
c=[]
for i in a:
if i in b:
c.append(i)
print(c)
Output:
[[2, 3, 6, 8], [15, 17, 21, 22]]
This should work, it should get you started -
import itertools
#Input lists
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
#Take a product of both the lists ( a X b )
z = itertools.product(a,b)
#Uncomment the following to see what itertools.product does
#[i for i in z]
#Return only the elements which the pair of the same element repeats (using string match)
[i[0] for i in z if str(i[0])==str(i[1])]
[[2, 3, 6, 8], [15, 17, 21, 22]]
one liner list comprehension approach:
dups = [i for i in a if i in b]
output:
[[2, 3, 6, 8], [15, 17, 21, 22]]
Try this:
a = [[2,3,6,8],[4,5,7,8,10],[15,17,21,22],[12,13,14,23,25]]
b = [[4,5,6],[15,17,21,22],[2,3,4],[2,3,6,8],[5,7,8,12,15],[7,12,14,17,32],[5,6,7,12,14]]
c = []
for i in a + b:
if (a + b).count(i) > 1 and i not in c:
c.append(i)
#mulaixi's answer is OK but in output list you may see duplicates.
Given a list containing monthly numerical data, how can I easily convert this into quarterly data?
x= [5,8,3,4,5,6,1,2,5,3,11,8] #monthly data for Jan-Dec
Desired output:
[5+8+3, 4+5+6, 1+2+5, 3+11+8] #converted to quarterly data
I wanted to do something like [a+b+c for a,b,c in x] but x it says x is not iterable.
I don't think this is a duplicate. I am specifically looking for a list comprehension solution.
A list comprehension way:
[sum([x[i],x[i+1],x[i+2]]) for i in range(0,len(x),3)]
#[16, 15, 8, 22]
Or in a nicer way (thanks #JonClements):
[sum(x[i:i+3]) for i in range(0, len(x), 3)]
And a numpy way:
import numpy as np
np.sum(np.array(x).reshape(-1,3),axis=1)
#array([16, 15, 8, 22])
Not getting to crazy you could just slice them up and then work with each group
x= [5,8,3,4,5,6,1,2,5,3,11,8]
a, b, c, d = x[:3], x[3:6], x[6:9], x[9:12]
print(a, b, c, d)
(xenial)vash#localhost:~/python/stack_overflow$ python3.7 slice_q.py
[5, 8, 3] [4, 5, 6] [1, 2, 5] [3, 11, 8]
From here if you want to sum each group you could do something like this
lista = [a, b, c, d,]
for i in lista:
print(sum(i))
16
15
8
22
Let's say I have:
a = [10,14,16]
b = [0,1,2]
and I want combine a and b into one list, as shown below:
print c
[[10, 0], [14, 1], [16, 2]]
I have tried to merge the two lists:
a + b
[10, 14, 16, 0, 1, 2]
but it's not same as what I want to achieve.
How can I do that in Python?
This is what zip() is for:
>>> a = [10,14,16]
>>> b = [0,1,2]
>>> zip(a, b)
[(10, 0), (14, 1), (16, 2)]
Note that this would get you a list of tuples. In case you want a list of lists:
>>> [list(item) for item in zip(a, b)]
[[10, 0], [14, 1], [16, 2]]
You could use zip built in function. It's very efficient compared to manual implementation.
In [52]: c = list(zip(a,b))
In [53]: c
Out[53]: [(10, 0), (14, 1), (16, 2)]
a = [10,14,16]
b = [0,1,2]
c = []
for i in range(len(a)):
c.append([a[i], b[i]])
print c
Or in one line:
print [[a[i], b[i]] for i in range(len(a))]
Outputs:
[[10, 0], [14, 1], [16, 2]]
using a simple for loop?
a = [10,14,16]
b = [0,1,2]
c = []
for i in range(len(a)):
try:
c.append([a[i],b[i]])
except KeyError:
c.append([a[i]])
print c
or by using a generator:
c = [ [a[i],b[i]] for i in range(len(a))]
for small lists you can do a "for" loop:
a = [10,14,16]
b = [0,1,2]
for i in range(len(a)):
out[i] = [a[i],b[i]]
Or for longer list you can use pandas to create a dataframe:
import pandas as pd
df = pd.dataframe([a,b],columns = ['a','b'])
out = df.T.values.tolist()
import numpy as np
np.array(a+b).reshape(2,3).T.tolist()
i have 2 lists
a=[[2,3,5],[3,6,2],[1,3,2]]
b=[4,2,1]
i want the output to be:
c=[[8,12,20],[6,12,4],[1,3,2]]
At present i am using the following code but its problem is that the computation time is very high as the number of values in my list are very large.The first list of list has 1000 list in which each list has 10000 values and the second list has 1000 values.Therefore the computation time is a problem.I want a new idea in which computation time is less.The present code is:
a=[[2,3,5],[3,6,2],[1,3,2]]
b=[4,2,1]
c=[]
s=0
for i in b:
c1=[]
t=0
s=s+1
for j in a:
t=t+1
for k in j:
if t==s:
m=i*k
c1.append(m)
c.append(c1)
print(c)
Use zip() to combine each list:
a=[[2,3,5],[3,6,2],[1,3,2]]
b=[4,2,1]
[[m*n for n in second] for m, second in zip(b,a)]
You can use numpy :
>>> import numpy as np
>>> a=np.array([[2,3,5],[3,6,2],[1,3,2]])
>>> b=np.array([4,2,1])
>>> a*np.vstack(b)
array([[ 8, 12, 20],
[ 6, 12, 4],
[ 1, 3, 2]])
Or as #csunday95 suggested as a more optimized way you can use transpose instead of vstack :
>>> (a.T*b).T
array([[ 8, 12, 20],
[ 6, 12, 4],
[ 1, 3, 2]])
This may not be faster, but it's a neater way of doing it :)
c = []
b_len = len(b)
for i in range(len(a)):
b_multiplier = b[i%b_len]
c.append([x*b_multiplier for x in a[i]])
Alternate short way now I've actually read the question properly and realised a and b are the same length:
c = [[x*b[i] for x in a[i]] for i in range(len(a))]