Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide each element in a tuple by a single integer? [closed]

Tags:

python

I want to divide a tuple by an integer. I am expecting something like this:

tuple = (10,20,30,50,80)
output = tuple/10
print(output)
output = (1,2,3,5,8)
like image 640
pedram.py Avatar asked Apr 03 '16 14:04

pedram.py


2 Answers

Basically the same as this answer by Truppo.

>>> t = (10,20,30)
>>> t2 = tuple(ti/2 for ti in t)
>>> t2
(5, 10, 15)
like image 200
Ben Lindsay Avatar answered Oct 24 '22 20:10

Ben Lindsay


may be you could try if is a numbers tuple:

numberstuple = (5,1,7,9,6,3)
divisor= 2.0
divisornodecimals = 2

value = map(lambda x: x/divisor, numberstuple)
>>>[2.5, 0.5, 3.5, 4.5, 3.0, 1.5]
valuewithout_decimals = map(lambda x: x/divisornodecimals, numberstuple)
>>>[2, 0, 3, 4, 3, 1]

or

value = [x/divisor for x in numberstuple]
like image 34
Milor123 Avatar answered Oct 24 '22 18:10

Milor123