0
Product of two list
I need to create combination of all elements between two list. I am using below code. from itertools import product L1 = [a,b,c], L2 = [1,2,3] comb = product(L1, L2) The problem is I have huge lists and number of combinations is around 30 lakhs...so I am not able to run this code. Any suggestions how to optimise ?
8 odpowiedzi
+ 6
I have done a short test concerning memory comsuption with the following code:
from itertools import product
L1 = range(25)
L2 = range(25)
count = 0 # for test only
for i in product(L1, L2):
count += 1 # for test only
print('finished', count) # for test only
Even if i used 25_000 elements per list, the memory consumption does not grow. But if you use a list to store the generated results, memory condumption grows as expexted.
Do you need the generated values in total to do something with them? If yes, you may run in the same problem as with your way of generation.
+ 5
Bishu Giri, just some questions: What are you going to do with the generated combinations? Do you store them? In a file?
What does it mean, when you say that you are not able to run the code. Is it that it takes too much time but finishes correctly, or does the app / python crashes? Or are you running in memory problems?
I am just thinking about what can be done, but therefore i need some answers on my questions. Thanks!
+ 2
If this is how your lists are constructed, could this be viable?
# If format of lists is this:
# a = list(range(1, x))
# b = list(range(x, y))
x = 10000
y = 10300
end_list = []
for i in range(1, x):
end_list.extend(list(range(x*i, y*i+1, i)))
# print(end_list) at your peril 😉
0
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 : please refer
a = [i for i in range(10000)]
b = [i for i in range(10000,10300)]
product(a,b)
0
Lothar : I have to calculate concordance (from logistic regression) ..for which I need these combinations...the code works perfectly in sample ..just that when I run this in bigger lists my system is crashing...I guess RAM problem (8gb)..I wanted to check if we can have optimized code for this.
- 1
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
result = [num1*num2 for num1, num2 in zip(a,b)]
print('Multiplication result is: ', result)
There are other methods also which can help you multiply two or more lists. You can find the full article here.....
https://programmersportal.com/how-to-multiply-two-lists-in-JUMP_LINK__&&__python__&&__JUMP_LINK/