0

Hey guys so i want to figure out how to multiply for example, if we have a string of [1,2,3,4] and another of [2,3,4,6], how do you multiply 1*2, 2*3, 3*4....so its each element of an array by the respective one of the second one? I also have these lists in float form if that makes it easier. My friend had suggested:

    file5 = open("New_list3",'r+')
    for line in file3:
        mult = float(line.strip())*float(file4.readline().strip())
        file5.write(mult+'\n')

Where file3 = the list of numerical string all on separate lines and file 4 the same but different values but that didn't work. Ideas? Each list is also very very long.

    [i*j for i,j in zip(file3,file4)] 
    file3=[i.rstrip() for i in file3] file4=[i.rstrip() 
    for i in file4] [float(i)*float(j) for i,j in zip(file3,file4)]
whuan0319
  • 61
  • 1
  • 1
  • 5

1 Answers1

5
[ i*j for i,j in zip(a,b)]

try this

file1=[i.rstrip() for i in file1]
file2=[i.rstrip() for i in file2]

[ float(i)*float(j) for i,j in zip(file1,file2)]

other way

for i,j in zip(file1,file2):
    out.write(float(i)*float(j) )
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46