user1541432
user1541432

Reputation: 11

I want to append a list of numbers to another list; using another list for the range of appending

Just to be clear there are 3 different lists involved. la is a list of integers and the posfinList is a list of numbers where each integer from la should be appended until it reaches the first number in the list then moves to the next in the posfinList. The numbers in posfinList will change everytime I use different data.

posfinList=[83, 81, 83, 82, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 86, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 85, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83]

la is a list of 6,932 integers

rosen = 0
blos=0
lafin = []
lafins=[]


while rosen<len(la):
    while rosen<(posfinList[blos]):
        lafin.append(la[rosen])
        if rosen >=(posfinList[blos]):
            lafins.append(lafin)

            blos+=1
        rosen+=1
print lafins

Upvotes: 1

Views: 616

Answers (2)

alcedine
alcedine

Reputation: 949

I'm assuming you want to chop up the list la into several pieces, so that the n:th piece has a length equal to posfinList[n] for all n. Please confirm/deny.


The following code is based on that assumption, see if it works the way you want:

lafins = []

for p in postfinList:
    lafins.append( la[rosen:rosen+p] )
    rosen += p

(If la[rosen:rosen+p] is unfamiliar notation to you, you can read about it (it's called 'slice' notation) in the Python Documentation. You'll find the relevant bits a little way down under the 'Strings' header.)

I have mostly used the same variable names as you, though they're not all that descriptive. Taking the time to choose variable names that actually document what the variables do generally pays off handsomely when testing and debugging, not to mention helps to make sure that you yourself know what those variables are meant to do.


As for your original code, there are at least three problems:

  • the stuff inside if rosen >=(posfinList[blos]) will never execute. The if itself is contained in while rosen<(posfinList[blos]), which actually makes sure that rosen>=(postfinList[blos]) will never be true inside it.

  • when while rosen<(posfinList[blos]) exits (unless by coincidence rosen>=len(la)), while rosen<len(la) will enter an infinite loop: rosen will always be less than len(la). Neither of those values will ever change again, so the while condition will always be true.

  • this one is difficult to explain, so I'm just going to say this: for your loop to work, in the example case you gave, postfinList should be [83, 164, 247, 329, ...] instead of [83, 81, 83, 82, ...], that is, it should be [83, 83+81, 83+81+83, 83+81+83+82, ...]. Think about why that is.

Upvotes: 1

ojchase
ojchase

Reputation: 1211

Well it's not entirely clear what you're trying to do. Could you clarify please? Or even better, what output would you expect for this input?:

la=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
posfinList=[2, 4, 6]

(StackOverflow people: Yes, I know that should be a comment, but I don't have the reputation for it yet!)

Even without a clarification, however, I can confirm that you have an infinite loop. If posfinList[0]<6932, as it is in your example, then the inner loop terminates with rosen=posfinList[0] and rosen has no way to increase or reset. The outer loop continues and never terminates, causing the infinite loop you're seeing.

Upvotes: 1

Related Questions