0

I would like to append an empty list within a for loop. Currently this is what I have:

iteration = 0
with open(logFile, 'r') as logfile_read:
    for line in logfile_read:
    line = line.rstrip()

    if 'Time' in line:
        iteration_time = re.findall(r'^Time\s+=\s+(.*)', line)

        for t in iteration_time:
            iteration = iteration +1
            print iteration, iteration_time

Here, iteration_time is a list varying from 0.1 to 0.5 in increments of 0.1. The output that I get with this is:

1 ['0.1']
2 ['0.2']
3 ['0.3']
4 ['0.4']
5 ['0.5']

However, I would like to assign an empty list to iteration such as iteration = [] and then be able to extend by 1 increment for every value of t within the for loop rather than using iteration = iteration +1.

8
  • Actually the list you showed is going from 0.1 to 0.5 , not from 0.0. Which is it? (Remember that enumerate() will start at 0, unless you use its optional second argument to change that) Commented Jan 21, 2015 at 0:38
  • Please show us all the code including the outer loop that extracts the line number by regex, and we can refactor all of this more elegantly. Commented Jan 21, 2015 at 0:43
  • 1
    what is the desired output ? it's not clear what do you really want to do. Commented Jan 21, 2015 at 0:43
  • Are you sure you want that line-number regex in the outer-loop, how is that all supposed to work if the line doesn't match 'Time\s+=...' ? Should we remember the last lineno and increment it? or else what? Commented Jan 21, 2015 at 0:50
  • The r'^Time\s+=\s+(.*)' will match because my log file will always have Time in it @smci Commented Jan 21, 2015 at 0:54

2 Answers 2

3

Is there any reason you don't just use the range() builtin function?

>>> for t in range(5+1): print t, t/10.
0 0.0
1 0.1
2 0.2
3 0.3
4 0.4
5 0.5

I don't see that you need to form the list. But if you do, use a list comprehension :

[(t, t/10.) for t in range(5+1)]
[(0, 0.0), (1, 0.1), (2, 0.2), (3, 0.3), (4, 0.4), (5, 0.5)]

Actually we should use enumerate() as @Amit says:

for t, tval in enumerate(t/10. for t in range(5+1)): print t,tval
... 
0 0.0
1 0.1
2 0.2
3 0.3
4 0.4
5 0.5

But in fact it looks like you want to start from 0.1, not 0.0. So you use the optional second arg to enumerate():

for t, tval in enumerate((t/10. for t in range(1,5+1)), 1): print t,tval
... 
1 0.1
2 0.2
3 0.3
4 0.4
5 0.5
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @smci. I could have used range(). However, this piece of code is within another for loop that strips every line of a file and extracts using regex. Hence, every time the value of range() would be 1.
Show us all your code with the outer loop, and we can refactor all of this more elegantly.
I have now added the entire loop. Thanks
2

Use enumerate to avoid counter:

for i, t in enumerate(iteration_time):
    print i+1, t

1 0.1
2 0.2
3 0.3
4 0.4
5 0.5

4 Comments

Do you want output exactly like 1 ['0.1'], where the second arg is a list of string, not a float? What's wrong with just a float? If you really want it, then do print "%d [%s]" % (i+1, t) Or print i+1, [str(t)]
I tried the way you put it @Amit. But I am getting 1 [0.1] 1 [0.2], etc.
@Deepak - You are probably missing something.
@Amit - No, I am doing exactly as you have

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.