1

I am now working on a python algorithm. I have a for loop to generate some numbers. These numbers will be used as indices to generate strings.

I have these codes:

val=4
ind=[]
for i in range(1,7):
        for j in range(val,val+i):
            val = val+1
            ind.append(j)
            if(i < 6):
                boundNode.insert(j, 'x%d' % (j-(2*i+1)))
            constraint_sty1 = str(boundNode[j]) + " + " + str(boundNode[j+1]) 
         val = j + 3

The problem here is: The boundNode[j] works to get what I demand, However, boundNode[j+1] encounters error, It seems that j+1 has been viewed as a sign to do some other operations here. I tried to only print(j+1), it can also print the indices I want. I'd like to know if there's any solution to this.

This is the output of print(boundNode[j]) and print(boundNode[j+1])

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 20 23 25 26 27 28
12 16 16 19 19 19 22 22 22 22 25 25 25 25 25 23 25 26 27 28 28

To define the larger picture, boundNode is a list like this:

boundNode = [0, 4, 6, 7, 12, 9, 16, 11, 19, 13, 22, 14, 25, 15, 20, 23, 25, 26, 27, 28, 28]

I use 2 for loops to generate indices like 4, 7, 8 , 11, 12 , 13 ... and put x1 to x15 into boundNode. And the boundNode is now like:

[0, 4, 6, 7, 'x1', 12, 9, 'x2', 'x3', 16, 11, 'x4', 'x5', 'x6', 19, 13, 'x7', 'x8', 'x9', 'x10', 22, 14, 'x11', 'x12', 'x13', 'x14', 'x15', 25, 15, 20, 23, 25, 26, 27, 28, 28]

What I am going to next is to use the indices to point to bound nodes and generate strings. Everything is going well except boundNode[j+1]

6
  • May be boundNode[j+1] gets overflow item Commented Dec 8, 2020 at 3:56
  • @toRex I'm not sure if it is overflow. I've edited the post. It shows the result of print(boundNode[j]) is more than print(boundNode[j+1]). Will this be useful to explain what happened? Commented Dec 8, 2020 at 4:02
  • Is 28 the last element of boundNode list? If so then after showing the last element of j, it shouldn't find j+1 index Commented Dec 8, 2020 at 4:09
  • @toRex Yes, 28 is the last element. But the problem is that the ideal result here is unique numbers while now it shows 1*12, 2*16, 3*19...This is not what i demand. Commented Dec 8, 2020 at 4:13
  • larger picture, what are you trying to do? What is boundNode? What is val? What is ind? Neither is defined in your question. Commented Dec 8, 2020 at 4:21

1 Answer 1

1

When the number of elements in boundNode is less than 7+val, you will get that error

IndexError: list index out of range

This error happens because you specify a range beyond the list. The corrected code would be:

val=5
ind=[]
boundNode = [0, 4, 6, 7, 12, 9, 16, 11, 19, 13, 22, 14, 25, 15, 20, 23, 25, 26, 27, 28, 28]
# boundNode = [0, 4, 6, 7, 'x1', 12, 9, 'x2', 'x3', 16, 11, 'x4', 'x5', 'x6', 19, 13, 'x7', 'x8', 'x9', 'x10', 22, 14, 'x11', 'x12', 'x13', 'x14', 'x15', 25, 15, 20, 23, 25, 26, 27, 28, 28]
boundNode = list(range(1,13))
for i in range(1,7):
    for j in range(val,val+i):
        val = val+1
        ind.append(j)
        if(i < 6):
            boundNode.insert(j, 'x%d' % (j-(2*i+1)))

        constraint_sty1 = str(boundNode[j]) + " + " + str(boundNode[j+1])
        print(constraint_sty1)

you will get results of the following:

x2 + 6
x1 + 6
x2 + 6
x1 + 6
x2 + 6
x3 + 6
x2 + 6
x3 + 6
x4 + 6
x5 + 6
x4 + 6
x5 + 6
x6 + 6
x7 + 6
x8 + 6
6 + 7
7 + 8
8 + 9
9 + 10
10 + 11
11 + 12
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer Kardi. But the problem here is not about the for loop. It's that I can successfully receive what i want in str(boundNode[j]) but not in the j+1 one. I guess the question should be how to avoid j+1 starting a new loop when pointing to a list.
Your question is actually unclear. First you said about the error, which has been answered here. Now you change the question about double for-loop. Why do you need to combine strings and integers in one array anyway? Separate them into two arrays would be simpler. You should probably explain what are you trying to accomplish in big picture. What should be the correct end result. Even if you did not achieve it in your code yet, at least you need to know your goal, which is not stated in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.