Python Forum
Variable info from a FOR loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable info from a FOR loop
#1
Hi all,
I have this FOR loop reading 8 channels from 3008 adc.
It runs eight times and thus i want 8 different values (ADC) as a result.

def read_tmp36(sensorid):
        adc = (8+sensorid)
        return adc

offset = 0
for offset in range(0,8):

      adc = read_tmp36(offset)

      print(adc)
      offset +=1
      
Never mind the code shown for it is only for experimenting but how can i get the 8 different values for the 8
channels out of this loop.
Or... should i use a totally different approach?
Reply
#2
def read_tmp36(sensorid):
    adc = (8+sensorid)
    return adc
 
for offset in range(0,8):
    adc = read_tmp36(offset)
    print(adc)
Reply
#3
Never mind people,
i licked the problem myself.
I used a indexed list.
def read_tmp36(sensorid):
        adc = (8+sensorid)
        return adc

lijst=[0,0,0,0,0,0,0,0]
for offset in range(0,8):
    adc = read_tmp36(offset)
    print(adc)
    lijst[offset]=adc
    offset +=1

print (lijst)
    
'lijst' neatly gives me the eight values.
Now i have to find out how to break down the list into individual numbers.
Reply
#4
To get a list you could have append the result in a list from @buran solution.
def read_tmp36(sensorid):
    adc = (8+sensorid)
    return adc

lst = []
for offset in range(0,8):
    adc = read_tmp36(offset)
    lst.append(adc)

print(lst)
Output:
[8, 9, 10, 11, 12, 13, 14, 15]
Or list comprehension.
def read_tmp36(sensorid):
    adc = (8+sensorid)
    return adc

print([read_tmp36(offset) for offset in range(0,8)])
Output:
[8, 9, 10, 11, 12, 13, 14, 15]
Reply
#5
As generator:

# dummy function to run the code
# this function should simulate the adc
read_adc = lambda x: (x + 2.6) * 1.8

def read_tmp(sensorid):
    return read_adc(8 + sensorid)
 
def read_all():
    for channel in range(8):
        yield read_tmp(channel)

# you define outside of the function which
# container object you create
all_sensor_data = list(read_all())
# the generator read_all() iterates until it's exhausted
# it's consumed by list
print(all_sensor_data)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 375 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,526 Nov-07-2023, 09:49 AM
Last Post: buran
  What is all the info in the info window in Idle? Pedroski55 3 647 Jul-08-2023, 11:26 AM
Last Post: DeaD_EyE
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,104 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,175 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,131 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,113 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,411 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Using Excel Cell As A Variable In A Loop knight2000 7 4,016 Aug-25-2021, 12:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020