Question

So I have a huge string and I process it element by element. I want to know how much is left so I try to take the current position word minus the last position word to see how many are left. I tried something like this:

with codecs.open("C:\\dna_chain.txt",'r', "utf-8") as file:
    for line in file:
        temp_buffer = line.split()
        for i,word in enumerate(temp_buffer):
            print(enumerate(temp_buffer[i]))
            print(enumerate(temp_buffer[-1]))

I get positions in memory and they are the same.

Was it helpful?

Solution

enumerate() returns a generator object; you only printed the representation of the object, then discard it again. The second line then reused the same memory location for a now object.

You can just refer directly to the temp_buffer object without enumerate() here:

for i, word in enumerate(temp_buffer):
    print(temp_buffer[i])
    print(temp_buffer[-1])

but temp_buffer[i] is the same thing as word in that loop. temp_buffer[-1] is the last word in your list.

If you wanted to know how many more words there are to process, use len(temp_buffer) - i, the length of the temp_buffer list, subtracting the current position.

OTHER TIPS

You should count the number of items beforehand:

words = len(temp_buffer)

in your code this would look like

import codecs

with codecs.open("C:\\dna_chain.txt",'r', "utf-8") as file:
    for line in file:
        temp_buffer = line.split()
        words = len(temp_buffer)
        for i,word in enumerate(temp_buffer):
            print(i, word, words-i-1)

this will print the index i, the word word and the number of remaining items in this row words-i-1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top