[Tutor] how to separate the digital and the alphabeta

Dave Angel d at davea.name
Thu Nov 3 18:09:32 CET 2011

On 11/03/2011 12:37 PM, lina wrote:
> <snip>
> I have another question, regarding the generator,
> def translate_process(dictionary,tobetranslatedfile):
>      results=[]
>      with open(tobetranslatedfile,"r") as f:
>          results=(dictionary[line.split()[2]] for line in f)
>          print(list(results))
>          print(len(list(results)))
> Here the print(list(results))
> did print a long list out,
> but why later print(len(list(results)))
> print 0 out?
> sorry, seems that way is very trivial.
> Best regards,
> _
resuts is a generator, and they can only be iterated through once.  So 
if you need it more than once, convert it to a list first, and save that 
list.  or just use a list comprehension in the first place, (just change 
the parenthese to square brackets)



More information about the Tutor mailing list