'In python how do i selectively combine/ print output 4 lists
in python v3 how do i get the prices of items based on given conditions
l3 -> (color price ) & l4->(articles base price ) & l1=(color name) & l2= (article name)
Ans::: should be like
orange - chair = 740, red - chair = 930... so on
l1= ['orange', 'red', 'black', 'blue' , 'white']
l2 = [ 'chair', 'table' , 'cup']
l3 = [240,430,650,700,900]
l4 = [500, 1000, 40]
Solution 1:[1]
I'm not sure if you want to have those Outputs separated by a ", " or a "\n".
But here it is:
l1 = ['orange', 'red', 'black', 'blue' , 'white']
l2 = ['chair', 'table' , 'cup']
l3 = [240, 430, 650, 700, 900]
l4 = [500, 1000, 40]
for i,elem2 in enumerate(l2):
for j,elem1 in enumerate(l1):
print(f"{elem1}-{elem2} = {l3[j]+l4[i]}" , end = ', ') # Separated by ", "
#print(f"{elem1}-{elem2} = {l3[j]+l4[i]}") # In differents lines
You can also use itertools to simplify these kind of problems like that:
import itertools
l1 = ['orange', 'red', 'black', 'blue' , 'white']
l2 = ['chair', 'table' , 'cup']
l3 = [240, 430, 650, 700, 900]
l4 = [500, 1000, 40]
for (j, elem2),(i, elem1) in itertools.product(enumerate(l2),enumerate(l1)):
print(f"{elem1}-{elem2} = {l3[j]+l4[i]}" , end = ', ')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TKirishima |
