Thank you very much everyone who helped answer. These all work as should and have been appendable. As no demonstrated below.
I am wanting to print working out of multiplication and addition.
import numpy as np
# [x, w] including bias
X = [[0.5, 0.15], [0.1, -0.3], [1, 0]]
in_str = 'in = '
for input in X:
substr = '('+str(input[0])+' x '+str(input[1])+') + '
in_str += substr
in_str = in_str[:-3]
print(in_str)
calcs = [x * y for x, y in X]
in_str = ' = '
for c in calcs:
substr = '('+str(c)+') + '
in_str += substr
in_str = in_str[:-3]
print(in_str)
ans = sum([x * y for x, y in X])
print(' = ' + str(ans))
Output:
in = (0.5 x 0.15) + (0.1 x -0.3) + (1 x 0)
= (0.075) + (-0.03) + (0)
= 0.045
Use list comprehension:
ans=sum([x*y for x,y in X])
If I understand your question correctly, You need value of product of elements in each sub list appended to a final list and sum of all elements in final list. Please find the below code. Traditional way
multiplier = 1
multiplier_list = []
final_sum = 0
for each_list in X:
for i in range(len(each_list)):
multiplier = multiplier * each_list[i]
#multiplier_list.append(multiplier) #If you need final list
final_sum = final_sum + multiplier
multiplier = 1
#print(multiplier_list)
print(final_sum)
X = [[0.5, 0.15], [0.1, -0.3], [1, 0]] # INPUT HERE print(f"in X = {X}") output ="" ans = 0 for input in X: mul=0 mul = input[0]*input[1] print(f"{input[0]} x {input[1]} = {mul}") ans+=mul output = output+f"{mul} + " output = output[:-2] output = output+f" = {ans}" print(output)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.