Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

added tag
Link
Jeru Luke
  • 21.5k
  • 13
  • 85
  • 91

one problem statement on python list arithmetic that is I have one list say,

my_set = [24565,24621,32,598,899]

my_set = [24565,24621,32,598,899]

I want to take difference between first two elements and if difference is in range of -127 to 127 then add +128 to next index position and so on. First element of the list stays as-is

Like this the output

[24565, 56, +128, 24589, +128, −566, +128, -301].

[24565, 56, +128, 24589, +128, −566, +128, -301].

this is what I tried to do

def inRange(val):
 return val in range(-127,127)
my_set = [24565,24621,32,598,899]
for i in range(len(my_set)-1):
 res = my_set[i] - my_set[i+1]
 if inRange(res) == True:
 my_set.insert(i+1,res)
 my_set.insert(i+2,128)
print(my_set) 

Please tell me how to do that.?? THankyou!

one problem statement on python list arithmetic that is I have one list say,

my_set = [24565,24621,32,598,899]

I want to take difference between first two elements and if difference is in range of -127 to 127 then add +128 to next index position and so on. First element of the list stays as-is

Like this the output

[24565, 56, +128, 24589, +128, −566, +128, -301].

this is what I tried to do

def inRange(val):
 return val in range(-127,127)
my_set = [24565,24621,32,598,899]
for i in range(len(my_set)-1):
 res = my_set[i] - my_set[i+1]
 if inRange(res) == True:
 my_set.insert(i+1,res)
 my_set.insert(i+2,128)
print(my_set) 

Please tell me how to do that.?? THankyou!

one problem statement on python list arithmetic that is I have one list say,

my_set = [24565,24621,32,598,899]

I want to take difference between first two elements and if difference is in range of -127 to 127 then add +128 to next index position and so on. First element of the list stays as-is

Like this the output

[24565, 56, +128, 24589, +128, −566, +128, -301].

this is what I tried to do

def inRange(val):
 return val in range(-127,127)
my_set = [24565,24621,32,598,899]
for i in range(len(my_set)-1):
 res = my_set[i] - my_set[i+1]
 if inRange(res) == True:
 my_set.insert(i+1,res)
 my_set.insert(i+2,128)
print(my_set) 

Please tell me how to do that.?? THankyou!

Source Link
Shriniwas
  • 762
  • 4
  • 13
  • 32

List arithmetic operation in python

one problem statement on python list arithmetic that is I have one list say,

my_set = [24565,24621,32,598,899]

I want to take difference between first two elements and if difference is in range of -127 to 127 then add +128 to next index position and so on. First element of the list stays as-is

Like this the output

[24565, 56, +128, 24589, +128, −566, +128, -301].

this is what I tried to do

def inRange(val):
 return val in range(-127,127)
my_set = [24565,24621,32,598,899]
for i in range(len(my_set)-1):
 res = my_set[i] - my_set[i+1]
 if inRange(res) == True:
 my_set.insert(i+1,res)
 my_set.insert(i+2,128)
 
print(my_set) 

Please tell me how to do that.?? THankyou!

lang-py

AltStyle によって変換されたページ (->オリジナル) /