Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping help
#5
I am not sure why you are repeating the identical block of code for appending to day1, day2, etc. for every if condition. Why not just write that once in a function?

There is an easier way though.

def store(root, index, first, second):
    onlist = eval(root+str(index))
    onlist.append(first)
    onlist.append(second)
That gives you a function you can call thus:

store('root', 1, num1, num2)
The root string could of course be a variable. The above would append the values of num1 and num2 to a list called root1. Instead of 1 you would probably have a loop that provides an index value that you could pass as the second argument:

for rootindex in range(1, 21):
        store('root', rootindex, num1, num2)
The above would append num1, and num2 to 20 lists called root1, root2, ... root20. I am using 'root' to me the root (core) part of the name of a variable and index to refer to the numeric differentiator on the end of root.

I am sure you can adjust this to refer to the different list roots (per and day) within appropriate nested loops.

Obviously, the use of lots of similarly named variables in the way you have said you have to work is not ideal. Normally, you would use a more suitable data structure normally.

You could of course not bother with the store function, and write it out directly:

eval('root'+str(rootindex)).append(num)
but I think the function makes things a little more clear.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
Looping help - by Wilson1218 - Jan-15-2018, 06:47 AM
RE: Looping help - by Gribouillis - Jan-15-2018, 06:57 AM
RE: Looping help - by Wilson1218 - Jan-15-2018, 07:09 AM
RE: Looping help - by wavic - Jan-15-2018, 09:54 AM
RE: Looping help - by gruntfutuk - Jan-15-2018, 10:22 AM
RE: Looping help - by Wilson1218 - Jan-15-2018, 10:50 AM
RE: Looping help - by gruntfutuk - Jan-15-2018, 11:24 AM
RE: Looping help - by buran - Jan-15-2018, 01:45 PM
RE: Looping help - by gruntfutuk - Jan-15-2018, 02:24 PM
RE: Looping help - by Wilson1218 - Jan-16-2018, 04:23 PM
RE: Looping help - by buran - Jan-15-2018, 03:19 PM
RE: Looping help - by gruntfutuk - Jan-15-2018, 03:23 PM
RE: Looping help - by Gribouillis - Jan-15-2018, 05:16 PM
RE: Looping help - by Wilson1218 - Jan-16-2018, 07:39 AM
RE: Looping help - by buran - Jan-16-2018, 04:49 PM
RE: Looping help - by Wilson1218 - Jan-16-2018, 05:07 PM
RE: Looping help - by nilamo - Jan-16-2018, 05:20 PM
RE: Looping help - by gruntfutuk - Jan-17-2018, 10:02 AM
RE: Looping help - by buran - Jan-16-2018, 05:46 PM
RE: Looping help - by Gribouillis - Jan-17-2018, 10:44 AM
RE: Looping help - by buran - Jan-17-2018, 11:00 AM
RE: Looping help - by nilamo - Jan-17-2018, 06:55 PM

Forum Jump:

User Panel Messages

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