Translation in python of C++ idiom

Sean 'Shaleh' Perry shalehperry at attbi.com
Fri Aug 9 18:17:32 CEST 2002


On 09-Aug-2002 Francois Petitjean wrote:
> In a paper entitled "Minimalism: Omit Needless Code" (1), Kevlin Henney
> advocates a concise coding style and for instance the following snippet :
> typedef std::istream_iterator<std::string> in;
> std::cout << std::distance(in(std::cin), in());
> is given to print the number of words in a textfile or stream. And by
> replacing the typedef by
> typedef std::istreambuf_iterator<char> in;
> we count chars.
> 
> To print the number of lines :
> typedef std::istreambuf_iterator<char> in;
> std::cout << std::count(in(std::cin), in(), '\n');
> 
> So, what would be the equivalent in Python?
> 

actually, most Python coders I see here would probably frown on this style of
coding.

import sys, string

len(sys.stdin.readlines()) # print number of lines on stdin

print len(string.join(sys.stdin.readlines(), '')) # print number of chars on
stdin

print len(string.split(string.join(sys.stdin.readlines(), ''))) # print number
of words on stdin




More information about the Python-list mailing list