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.
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.