Search code examples
stringlistdelimiter

Cartesian product from list of Strings


To answer this question you need to write code that will produce a list of strings that contains all combinations that can be made from two input lists by taking an element from the first list and following it by an element of the second list (with a space in between).

The ordering of elements in the new list should be determined primarily the the ordering in the first list and secondarily (for elements with the same same first part) by the ordering of the second list. So, if the first input is of the form: A, B, .. X and the second is of the form: 1, 2, .. n the output would be of the form:
["A 1", "A 2", .. "An", "B 1", "B 2", .. "Bn", .. "X 1", "X 2", .. "Xn"]

(where the ".." indicates there can be more elements in between).

NOTES: 1. There can be different numbers of elements in the two lists. 2. If the first list has M elements and the second list has N elements, then the output list should have M*N elements.

""" )

print("INPUT:")
listA=[] #Creating a list
listB=[]#Creating a second list
listC=[]


while True:
        print("add element y to continue")
        if input()!='y':
            break
        else:
            print("keep adding or x to break" )
            while True:
                if input=='x':
                    break
                else:
                    input_listA = input( "Enter first comma separated list of strings: " )

                    print(n)
                    listA.append(input_listA)
                    print(input_listA)
                    print(listA)
                if input=='x':
                    break
                else:
                    input_listB=input( "Enter second comma separated list of int: " )
                    input_listB.split()
                    listB.append(input_listB)
                    break

however when i right words it for instance ["black ham ", "twice mouse", "floating cheese", "blue elf", "electric elf", "floating elf"] in the calculation for the cartesian product will be calculatin characters not words like 'b', 'l','a','c','k' how can i make the strings words inside the list and also how can i print it in the form of i mean without commas because as you can see i input delimiter along the strings


Solution

  • Try this:

    import itertools
    
    a = input("Enter first comma separated list of strings: ")
    b = input("Enter second comma separated list of strings: ")
    result = []
    for i in itertools.product(a.split(","), b.split(",")):
      result.append("".join(map(str, i)))
    
    print(result)
    

    Result:

    ~ $ python3 test.py
    Enter first comma separated list of strings: aa,bb
    Enter second comma separated list of strings: 1,2,3
    ['aa1', 'aa2', 'aa3', 'bb1', 'bb2', 'bb3']
    

    If you'd rather like a space between the two words in each pair, change

    result.append("".join(map(str, i)))
    

    to

    result.append(" ".join(map(str, i)))
    

    Output:

    ['aa 1', 'aa 2', 'aa 3', 'bb 1', 'bb 2', 'bb 3']