Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping help
#1
Hello, I need help looping some code. The problem is that there are many lists that I have to .append to and they are all named similarly, but different. Can I loop this code?
Note: All names of lists must remain the same.
Note 2: per_no is a count in a loop that this code is in.

        yeild1 = float(input("Yield 1"))
        yeild2 = float(input("Yield 2"))
        if per_no == 1:
            per1.append(yeild1)
            per1.append(yeild2)
            if day == 1:
                day1.append(yeild1)
                day1.append(yeild2)
            elif day == 2:
                day2.append(yeild1)
                day2.append(yeild2)
            elif day == 3:
                day3.append(yeild1)
                day3.append(yeild2)
            elif day == 4:
                day4.append(yeild1)
                day4.append(yeild2)
            elif day == 5:
                day5.append(yeild1)
                day5.append(yeild2)
            elif day == 6:
                day6.append(yeild1)
                day6.append(yeild2)
            elif day == 7:
                day7.append(yeild1)
                day7.append(yeild2)
        elif per_no == 2:
            per2.append(yeild1)
            per2.append(yeild2)
            if day == 1:
                day1.append(yeild1)
                day1.append(yeild2)
            elif day == 2:
                day2.append(yeild1)
                day2.append(yeild2)
            elif day == 3:
                day3.append(yeild1)
                day3.append(yeild2)
            elif day == 4:
                day4.append(yeild1)
                day4.append(yeild2)
            elif day == 5:
                day5.append(yeild1)
                day5.append(yeild2)
            elif day == 6:
                day6.append(yeild1)
                day6.append(yeild2)
            elif day == 7:
                day7.append(yeild1)
                day7.append(yeild2)
        elif per_no == 3:
            per3.append(yeild1)
            per3.append(yeild2)
            if day == 1:
                day1.append(yeild1)
                day1.append(yeild2)
            elif day == 2:
                day2.append(yeild1)
                day2.append(yeild2)
            elif day == 3:
                day3.append(yeild1)
                day3.append(yeild2)
            elif day == 4:
                day4.append(yeild1)
                day4.append(yeild2)
            elif day == 5:
                day5.append(yeild1)
                day5.append(yeild2)
            elif day == 6:
                day6.append(yeild1)
                day6.append(yeild2)
            elif day == 7:
                day7.append(yeild1)
                day7.append(yeild2)
        elif per_no == 4:
            per4.append(yeild1)
            per4.append(yeild2)
            if day == 1:
                day1.append(yeild1)
                day1.append(yeild2)
            elif day == 2:
                day2.append(yeild1)
                day2.append(yeild2)
            elif day == 3:
                day3.append(yeild1)
                day3.append(yeild2)
            elif day == 4:
                day4.append(yeild1)
                day4.append(yeild2)
            elif day == 5:
                day5.append(yeild1)
                day5.append(yeild2)
            elif day == 6:
                day6.append(yeild1)
                day6.append(yeild2)
            elif day == 7:
                day7.append(yeild1)
                day7.append(yeild2)
Reply
#2
If day1, day2, ... are global variables, you can do
dayi = globals()['day{}'.format(i)] 
dayi.extend((yeild1, yeild2))
Obviously, a single day list would be better. You could define
day = [None, day1, day2, day3, day4, day5, day6, day7]
...
day[i].extend((yeild1, yeild2))
Reply
#3
(Jan-15-2018, 06:57 AM)Gribouillis Wrote: If day1, day2, ... are global variables, you can do
dayi = globals()['day{}'.format(i)] 
dayi.extend((yeild1, yeild2))
Obviously, a single day list would be better. You could define
day = [None, day1, day2, day3, day4, day5, day6, day7]
...
day[i].extend((yeild1, yeild2))

The 'day' things are lists, and I cannot use two dimensional lists in this task. I would also like to add that if I could somehow call a list name in a loop that would be great!
Example: Instead of day1, day2, day3 etc I would use something to the effect of day+ count_no
Reply
#4
Can you use dictionaries?

per_num = {key: value for key, value in enumerate([per1, per2, per3, per4], 1)}
days = {key: value for key, value in enumerate([day1, day2, day3, day4, day5, day6, day7], 1)}

yeild1 = float(input("Yield 1"))
yeild2 = float(input("Yield 2"))

per_num[per_no].extend([yeild1, yeild2])
days[day].extend([yeild1, yeild2])
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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
#6
(Jan-15-2018, 09:54 AM)wavic Wrote: Can you use dictionaries?

per_num = {key: value for key, value in enumerate([per1, per2, per3, per4], 1)}
days = {key: value for key, value in enumerate([day1, day2, day3, day4, day5, day6, day7], 1)}

yeild1 = float(input("Yield 1"))
yeild2 = float(input("Yield 2"))

per_num[per_no].extend([yeild1, yeild2])
days[day].extend([yeild1, yeild2])


No, sorry!

(Jan-15-2018, 10:22 AM)gruntfutuk Wrote: 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.

This is perfect, if it works. So I would enter "per",count,yeild1,yeild2 for example?
Reply
#7
(Jan-15-2018, 10:50 AM)Wilson1218 Wrote: This is perfect, if it works. So I would enter "per",count,yeild1,yeild2 for example?
Yes.

I strongly recommend you try this out in the interactive shell first and make sure you understand it. Print out the id of each list your create, print(id(alistvar)) including the list in the function.

PS. It does work. I rewrote your code in just a few lines, and tested it.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#8
It is really, really bad idea to teach this to newbie students...
Reply
#9
(Jan-15-2018, 01:45 PM)buran Wrote: It is really, really bad idea to teach this to newbie students...
I'm a relative newbie, so I need you to explain why that is.

I couldn't see another way of referencing the variables when the OP stated that a different data design could not be used.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#10
Gribouillis already showed one way, using globals(). Other option is using vars(). If we go OOP, there are other options too
In any case I doubt OP restrictions on using dict, more over they don't provide the full code. Also, that is why we usually request to post the full homework assignment.
Reply


Forum Jump:

User Panel Messages

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