Home > Back-end >  How to create lists from a cartesian product of two string lists
How to create lists from a cartesian product of two string lists

Time:11-30

I have two string lists that look like this:

print(objects_list)
print(verb_list)

they output:

['parking_meter', 'sink', 'teddy_bear']
 
 ['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install',
 'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze',
 'stab', 'straddle', 'talk_on']

I want product of these two lists in a format that looks like this:

[['parking_meter','sail'],['parking_meter','fill'],['parking_meter','fly']......]

I tried this code

list3 = [[str(l),str(n)] for l in objects_list for n in verb_list]
print(list3)

but it outputs:

[['[', '['], ['[', "'"], ['[', 's'], ['[', 'a'], ['[', 'i'], ['[',
 'l'], ['[', "'"], ['[', ','], ['[', ' '], ['[', "'"], ['[', 'f'],
 ['[', 'i'], ['[', 'l'], ['[', 'l'], ['[', "'"], ['[', ','], ['[', '
 '], ['[', "'"], ['[', 'f'], ['[', 'l'], ['[', 'y'], [.....

any solution?

CodePudding user response:

Your code should work as is. I suspect you have some other unnecessary str() function calls in your actual code:

objects_list = ['parking_meter', 'sink', 'teddy_bear']
verb_list = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose',
             'hunt', 'install', 'kick', 'launch', 'move', 
             'pick', 'repair', 'sit_at', 'squeeze', 'stab', 
             'straddle', 'talk_on']

list3 = [[o,v] for o in objects_list for v in verb_list]

[['parking_meter', 'sail'], ['parking_meter', 'fill'], ['parking_meter', 'fly'], ['parking_meter', 'greet'], ['parking_meter', 'hit'], ['parking_meter', 'hose'], ['parking_meter', 'hunt'], ['parking_meter', 'install'], ['parking_meter', 'kick'], ['parking_meter', 'launch'], ['parking_meter', 'move'], ['parking_meter', 'pick'], ['parking_meter', 'repair'], ['parking_meter', 'sit_at'], ['parking_meter', 'squeeze'], ['parking_meter', 'stab'], ['parking_meter', 'straddle'], ['parking_meter', 'talk_on'], ['sink', 'sail'], ['sink', 'fill'], ['sink', 'fly'], ['sink', 'greet'], ['sink', 'hit'], ['sink', 'hose'], ['sink', 'hunt'], ['sink', 'install'], ['sink', 'kick'], ['sink', 'launch'], ['sink', 'move'], ['sink', 'pick'], ['sink', 'repair'], ['sink', 'sit_at'], ['sink', 'squeeze'], ['sink', 'stab'], ['sink', 'straddle'], ['sink', 'talk_on'], ['teddy_bear', 'sail'], ['teddy_bear', 'fill'], ['teddy_bear', 'fly'], ['teddy_bear', 'greet'], ['teddy_bear', 'hit'], ['teddy_bear', 'hose'], ['teddy_bear', 'hunt'], ['teddy_bear', 'install'], ['teddy_bear', 'kick'], ['teddy_bear', 'launch'], ['teddy_bear', 'move'], ['teddy_bear', 'pick'], ['teddy_bear', 'repair'], ['teddy_bear', 'sit_at'], ['teddy_bear', 'squeeze'], ['teddy_bear', 'stab'], ['teddy_bear', 'straddle'], ['teddy_bear', 'talk_on']]

CodePudding user response:

Try this, use the itertools library. There is a function called product that gets you a cartesian product.

import itertools

x = ['parking_meter', 'sink', 'teddy_bear']

y = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install', 'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze', 'stab', 'straddle', 'talk_on']

Then you can do this 2-liner, (because the product function returns a tuple but you require a list)

z = list(itertools.product(x,y))
z = [list(z) for z in z]

Or this 1-liner,

z = [list(tup) for tup in list(itertools.product(x, y))]

Then I,

print (z)

Which gives me

[['parking_meter', 'sail'],
 ['parking_meter', 'fill'],
 ['parking_meter', 'fly'],
 ['parking_meter', 'greet'],
 ['parking_meter', 'hit'],
 ['parking_meter', 'hose'],
 ['parking_meter', 'hunt'],
 ['parking_meter', 'install'],
 ['parking_meter', 'kick'],
 ['parking_meter', 'launch'],
 ['parking_meter', 'move'],
 ['parking_meter', 'pick'],
 ['parking_meter', 'repair'],
 ['parking_meter', 'sit_at'],
 ['parking_meter', 'squeeze'],
 ['parking_meter', 'stab'],
 ['parking_meter', 'straddle'],
 ['parking_meter', 'talk_on'],
 ['sink', 'sail'],
 ['sink', 'fill'],
 ['sink', 'fly'],......

CodePudding user response:

From the output you get, it seems your input lists are strings, not lists.

As if objects_list was the output of print(objects_list), not the list itself.

Therefore, when iterating over objects_list, rather than interating over list items, you are iterating over the characters of its string reprensentation:

"["
"'"
"p"
"a"
...

Your code is otherwise correct, except you don't need to cast the elements to string in the list comprehension.

list3 = [[l, n] for l in objects_list for n in verb_list]

CodePudding user response:

Your code worked just fine and gave me the expected output. I am not sure is there any other problem. May be your list is not declared like me...if you had created this list from another source...you should check the format.

Note: I have run this code on Python 3.9.5

objects_list = ['parking_meter', 'sink', 'teddy_bear']
verb_list = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install', 'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze', 'stab', 'straddle', 'talk_on'] 

list3 = [[str(l),str(n)] for l in objects_list for n in verb_list] 
print(list3)

Output:
[['parking_meter', 'sail'], ['parking_meter', 'fill'], ['parking_meter', 'fly']['parking_meter', 'greet'], ['parking_meter', 'hit'], ['parking_meter', 'hose'], ['parking_meter', 'hunt'], ['parking_meter', 'install'], ['parking_meter', 'kick'], ['parking_meter', 'launch'], ['parking_meter', 'move'], ['parking_meter', 'pick'], ['parking_meter', 'repair'], ['parking_meter', 'sit_at'], ['parking_meter', 'squeeze'], ['parking_meter', 'stab'], ['parking_meter', 'straddle'], ['parking_meter', 'talk_on'], ['sink', 'sail'], ['sink', 'fill'], ['sink', 'fly'], ['sink', 'greet'], ['sink', 'hit'], ['sink', 'hose'], ['sink', 'hunt'], ['sink', 'install'], ['sink', 'kick'], ['sink', 'launch'], ['sink', 'move'], ['sink', 'pick'], ['sink', 'repair'], ['sink', 'sit_at'], ['sink', 'squeeze'], ['sink', 'stab'], ['sink', 'straddle'], ['sink', 'talk_on'], ['teddy_bear', 'sail'], ['teddy_bear', 'fill'], ['teddy_bear', 'fly'], ['teddy_bear', 'greet'], ['teddy_bear', 'hit'], ['teddy_bear', 'hose'], ['teddy_bear', 'hunt'], ['teddy_bear', 'install'], ['teddy_bear', 'kick'], ['teddy_bear', 'launch'], ['teddy_bear', 'move'], ['teddy_bear', 'pick'], ['teddy_bear', 'repair'], ['teddy_bear', 'sit_at'], ['teddy_bear', 'squeeze'], ['teddy_bear', 'stab'], ['teddy_bear', 'straddle'], ['teddy_bear', 'talk_on']]
  • Related