1
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

hydthemoon
  • 162
  • 1
  • 2
  • 11
  • Even though [there are no tuple comprehensions](https://stackoverflow.com/questions/16940293/why-is-there-no-tuple-comprehension-in-python) in Python, this wouldn't be syntactically valid for any type of generator. – chepner Nov 24 '19 at 14:05

2 Answers2

4

Try this :

tup = tuple(int(i/j) for i,j in zip(tup1, tup2))

Output :

(50,50,50)

NOTE :

  1. When you do (x/y for x in tup1, y in tup2) you are trying to create a generator.
  2. Again (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.
  3. You need 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.
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
1

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.])
Viach
  • 498
  • 4
  • 10