Append to python List

88888 Dihedral dihedral88888 at googlemail.com
Thu May 9 07:24:53 EDT 2013


Jussi Piitulainen於 2013年5月9日星期四UTC+8下午2時55分20秒寫道:
> RAHUL RAJ writes:
> 
> 
> 
> > Checkout the following code:
> 
> > 
> 
> > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y]   
> 
> > output=[]
> 
> > output=[x for x in sample2 if x not in output]
> 
> > 
> 
> > the output I get is
> 
> > 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11
> 
> > 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16
> 
> > 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17
> 
> > 
> 
> > which contains duplicate values.
> 
> 
> 
> The second comprehension, [x for x in sample2 if x not in output], in
> 
> the context, is equivalent to [x for x in sample2 if x not in []]. It
> 
> does not refer to an incomplete version of the list that gets assigned
> 
> to the variable after it's done.

This is just the handy style for a non-critical loop.
In a critical loop, the number of  the total operation counts
does matter in the execution speed.



More information about the Python-list mailing list