Home > other >  This problem is related to list in python
This problem is related to list in python

Time:01-16

can someone help me this problem? I don't know how to describe this. My Code:

i = [30,40,50]
k = [10,20,30]
d = []

I want this in my OUTPUT:

d = [[30,10], [30, 20], [30, 30], [40, 10], [40, 20], [40, 30], [50, 10], [50, 20], [50, 30]]

CodePudding user response:

use product form itertools

i = [30,40,50]
k = [10,20,30]

from itertools import product
d = list(product(i, k))
print(d)
# [(30, 10), (30, 20), (30, 30), (40, 10), (40, 20), (40, 30), (50, 10), (50, 20), (50, 30)]

CodePudding user response:

i = [30,40,50]
k = [10,20,30]
d = []
for a in i:
    for b in k:
        d.append([a, b])

print(d)

CodePudding user response:

for p in range(len(i)):        
    for q in range(len(k)):
        d.append([i[p],k[q])
print(d)

CodePudding user response:

import itertools
i = [30,40,50]
k = [10,20,30]
list_tuple = list(itertools.product(i, k))
d = [list(t) for t in list_tuple ]
print(d)
#output
[[30, 10], [30, 20], [30, 30], [40, 10], [40, 20], [40, 30], [50, 10], [50, 20], [50, 30]]

CodePudding user response:

If you don't want to use one the libraries, just write a list comprehension:

output = [[x, y] for x in i for y in k]

print(output)

OUTPUT

[[30, 10], [30, 20], [30, 30], [40, 10], [40, 20], [40, 30], [50, 10], [50, 20], [50, 30]]
  •  Tags:  
  • Related