Question

In: Computer Science

Here is the problem I am working on. I need to write a py program that:...

Here is the problem I am working on. I need to write a py program that:

Calculates a Cartesian Product Output, A x B of 2 lists. I want to limit each list to 5 numbers. It is kind of working, but I can't help think that there is a simpler way to express this problem. Please advise...

Here is my code:

def CartesianProductOutput(A,B):

    lst = []

    for x in A:

        t = []

        t.append(x)

        for y in B:

            temp = []

            for x in t:

                temp.append(x)

            temp.append(y)

            lst.append(temp)

    return lst

A = [1,2,3,4,5]

B = [1,2,3,4,5]

print(CartesianProductOutput(A,B))

Solutions

Expert Solution

HELLO!!

EXPLANATION:

  • Through "itertools" module you can make iterative tasks like in our problem
  • The function "product" is used to find the cartesian product of the given lists

CODE:

#importing product function from itertools module
from itertools import product
a=[1,2,3,4,5]           
b=[1,2,3,4,5]
x=[]          #an empty list 
product(a,b)  #using product function
for each in product(a,b):  #loop to append to the list
    x.append(each)      #appending to the empty list x
print(x)               #printing the list

OUTPUT:


Related Solutions

Here is a Problem I need to solve: 5) Write a function to determine is a...
Here is a Problem I need to solve: 5) Write a function to determine is a given word is legal. A word is illegal if it contains no vowels. For this problem, the letter Y is considered to be a legal vowel. Pass in a word to this function and it will determine if the word is legal or not. You can make the following assumptions about the word you a passing to this function. 1) The string being passed...
I am working on this problem for the company AT & T and am not sure...
I am working on this problem for the company AT & T and am not sure how to start it. Draw a chart of the main inter-organizational linkage mechanisms (e.g., long -term contacts, strategic alliances, mergers) that your organization uses to manage its symbiotic resource interdependencies. Using resource dependence theory and transaction cost theory, discuss why the organization to manage its interdependencies in this way. Do you think the organization has selected the most appropriate linkage mechanisms? Why or why...
Here is the scenario that I am working on: The efficacy of a new addiction medication...
Here is the scenario that I am working on: The efficacy of a new addiction medication was evaluated in a randomized, placebo-controlled, doubleblind study. The medication in question, Antaquil, is intended to moderate the symptoms of alcohol withdrawal and craving with minimum side effects. Over the course of three weeks, a sample of 36 individuals who were recovering from alcohol addiction were randomly assigned to two groups: one administered the medication and one administered a placebo. At the end of...
Here is the scenario that I am working on: The efficacy of a new addiction medication...
Here is the scenario that I am working on: The efficacy of a new addiction medication was evaluated in a randomized, placebo-controlled, doubleblind study. The medication in question, Antaquil, is intended to moderate the symptoms of alcohol withdrawal and craving with minimum side effects. Over the course of three weeks, a sample of 36 individuals who were recovering from alcohol addiction were randomly assigned to two groups: one administered the medication and one administered a placebo. At the end of...
Here is the scenario that I am working on: The efficacy of a new addiction medication...
Here is the scenario that I am working on: The efficacy of a new addiction medication was evaluated in a randomized, placebo-controlled, doubleblind study. The medication in question, Antaquil, is intended to moderate the symptoms of alcohol withdrawal and craving with minimum side effects. Over the course of three weeks, a sample of 36 individuals who were recovering from alcohol addiction were randomly assigned to two groups: one administered the medication and one administered a placebo. At the end of...
Here is the scenario that I am working on: The efficacy of a new addiction medication...
Here is the scenario that I am working on: The efficacy of a new addiction medication was evaluated in a randomized, placebo-controlled, doubleblind study. The medication in question, Antaquil, is intended to moderate the symptoms of alcohol withdrawal and craving with minimum side effects. Over the course of three weeks, a sample of 36 individuals who were recovering from alcohol addiction were randomly assigned to two groups: one administered the medication and one administered a placebo. At the end of...
Here is a project I am currently working on and I could use some feedback on...
Here is a project I am currently working on and I could use some feedback on how to get started. I calculated the general probabilities for the first two columns of each spreadsheet (successes / total outcomes). I don't want to move forward until I know I'm on the right track but I'm concerned that I've oversimplified the question being asked. I'm also using Excel and am having a hard time finding an appropriate formula for conditional probability. My book...
I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
I am currently working on this problem. It says it is wrong, and I was wondering...
I am currently working on this problem. It says it is wrong, and I was wondering if someone could correct my code to where its ouput is as followed. Expected Output: 6↵ 5↵ 4↵ 3↵ 2↵ 1 Actual Output: 9↵ 8↵ 7↵ 6↵ 5↵ 4↵ 3↵ 2↵ 1↵ 0↵ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize these entries and post the closing entries 2. i need to prepare Dalhanis multi-step income statement and statement of owners equity for August 2010 3. i need to prepare the blance sheet at august 31,2010 4. i need to prepare a post-closing trial balance at august 31,2010 DALHANI makes all credit sales on terms 2/10 n/30 and uses the Perpetual Inventory System Aug 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT