Write a Python Program That Calculates The Dot Product of Two Lists Containing The Same Number of Elements

Hi in this article you will learn about a dot product of two python lists containing integers,  so let's assume integer numbers  present in the list and let's try to calculate the dot product of two lists.


Write a Python Program That Calculates The Dot Product of Two Lists Containing The Same Number of Elements

 

Write a Python Program That Calculates The Dot Product of Two Lists Containing The Same Number of Elements

Let's define a function named as dot_product by passing two  list arguments a and b,  these are the two list variables that I'll be passing as an input to the Python function.

Now let's take a temporary variable named as product to store the dot product of two lists  and initialize it to zero. Now use the for loop to iterate all the items present in the to list by using a zip()  function call and take the i and j elements present in the list.

Inside the for loop calculate the dot product by multiplying I and J value and add it to the product variable. The product variable will store the dot product of two lists, finally returning the product variable.
def dot_product(a, b):
   product = 0
   for i, j in zip(a, b):
       product = product + (i * j)
   return product





In the main program take two variables named as first and second to store the two lists. Ask the user to enter a list separated by the spaces using the input function call and then convert it into an integer type using map() function and then again convert it into an list().

Check if the length of both the list variables are the same then take a result variable and call the function by passing the first and second list. Once the function gets executed the result variable will store the dot product of two lists so use the print() function call to print the dot product of two lists.

Use the else block to print an error message saying to list or not of the same length and exit the program gracefully.


first = list(map(int, input("Enter First List (Separated by space): ").split()))
second = list(map(int, input("Enter First List (Separated by space): ").split()))
if len(first) == len(second):
   result = dot_product(first, second)
   print("The dot product of two list is : ", result)
else:
   print("The length of two list is not same")





Complete Python Program
=========================

def dot_product(a, b):
   product = 0
   for i, j in zip(a, b):
       product = product + (i * j)
   return product


first = list(map(int, input("Enter First List (Separated by space): ").split()))
second = list(map(int, input("Enter First List (Separated by space): ").split()))
if len(first) == len(second):
  result = dot_product(first, second)
  print("The dot product of two list is : ", result)
else:
  print("The length of two list is not same")



Output 1
================

Enter First List (Separated by space): 1 2 3
Enter First List (Separated by space): 1 2 3
The dot product of two list is :  14



Output 2
================

Enter First List (Separated by space): 12 34
Enter First List (Separated by space): 1
The length of two list is not same


Output 3
================

Enter First List (Separated by space): 1 2 3 4 5 6
Enter First List (Separated by space): 9 1 3 4 8 9
The dot product of two list is :  130



Output 4
================

Enter First List (Separated by space): 3 7 8
Enter First List (Separated by space): 1 5 9
The dot product of two list is :  110



The other variant of dot_product() function uses a for loop by iterating the index using range() function and calculates the dot product.

def dot_product(a, b):
   product = 0
   for index in range(len(a)):
       product = product + (a[index] * b[index])
   return product


first = list(map(int, input("Enter First List (Separated by space): ").split()))
second = list(map(int, input("Enter First List (Separated by space): ").split()))
if len(first) == len(second):
  result = dot_product(first, second)
  print("The dot product of two list is : ", result)
else:
  print("The length of two list is not same")




If the problem statement changes to calculate the dot product of the floating point list then just  change the type conversion from integer to floating point number as shown below.

def dot_product(a, b):
   product = 0
   for index in range(len(a)):
       product = product + (a[index] * b[index])
   return product


first = list(map(float, input("Enter First List (Separated by space): ").split()))
second = list(map(float, input("Enter First List (Separated by space): ").split()))
if len(first) == len(second):
  result = dot_product(first, second)
  print("The dot product of two list is : ", result)
else:
  print("The length of two list is not same")



Floating Point Output
=======================

Enter First List (Separated by space): 1.1 2.2 3.3
Enter First List (Separated by space): 4.4 5.5 6.7
The dot product of two list is :  39.05


Floating Point Output 2
=======================

Enter First List (Separated by space): 1.3
Enter First List (Separated by space): 5.7 8.9
The length of two list is not same




Complete Program Statement
==========================
 
Write a python program that calculates the dot product of two lists containing the same number of elements. The program should take two lists of integers as input from the user, and then call a function named dot_product to find the dot product of the two lists


The dot_product function should take two lists a and b as input, and should return the dot product of those two lists. Your program should first prompt the user to enter the two lists of integers, which should be entered as strings separated by spaces.

Your program should then convert the input strings into lists of integers. Your program should then call the dot_product function with the two lists as arguments, and store the resulting dot product in a variable named result. Finally, Your program should print out the dot product of the two lists as the output.

You can assume that the two input lists will always contain the same number of elements. Your program should output an error message and exit gracefully if the two lists have different lengths. In your implementation, you should define the dot_product function using a loop to calculate the dot product of the two input lists



Conclusion
==================
Execute the above Python program by providing to integer list  having same  length as a input to the script,  and find out the dot product of two list




Post a Comment

0 Comments