tup1 = (100,200,300)
tup2 = (2,4,6)
tup = (x/y for x in tup1, y in tup2)
or
so i expected
tup = (50,50,50)
but there comes
SyntaxError: invalid syntax
for that comma
tup1 = (100,200,300)
tup2 = (2,4,6)
tup = (x/y for x in tup1, y in tup2)
or
so i expected
tup = (50,50,50)
but there comes
SyntaxError: invalid syntax
for that comma
Try this :
tup = tuple(int(i/j) for i,j in zip(tup1, tup2))
Output :
(50,50,50)
NOTE :
(x/y for x in tup1, y in tup2)
you are trying to create a generator.(x/y for x in tup1, y in tup2)
is not valid double loop operation. You may have tried (x/y for x in tup1 for y in tup2)
but again it would create a generator object and even if you mapped it to tuple format it would provide (50.0, 25.0, 16.666666666666668, 100.0, 50.0, 33.333333333333336, 150.0, 75.0, 50.0)
- wrong because the way the double loop is operating.int(x/y)
in stead of only i/j
because even though i
and j
are integers, the division operation would turn the result into a floating point.if You are going to work with big numbers, then a more effective way is to use Pandas or NumPy. It is easier and takes less time and resources.
#pandas
import pandas as pd
tup1 = (100,200,300)
tup2 = (2,4,6)
df = pd.DataFrame({'tup1': tup1, 'tup2': tup2} )
df
tup1 tup2
0 100 2
1 200 4
2 300 6
df['tup1'] / df['tup2']
0 50.0
1 50.0
2 50.0
dtype: float64
#numpy
import numpy as np
tup1 = (100,200,300)
tup2 = (2,4,6)
arr = np.array([tup1, tup2])
arr[0] / arr[1]
array([50., 50., 50.])