只是重复一下
sys.stdin
,它将在行上迭代。
map
filter
如果你愿意的话。进入的每一行都将通过管道,在此过程中不会生成任何列表。
以下是每种方法的示例:
import sys
stripped_lines = (line.strip() for line in sys.stdin)
lines_with_prompt = ('--> ' + line for line in stripped_lines)
uppercase_lines = map(lambda line: line.upper(), lines_with_prompt)
lines_without_dots = filter(lambda line: '.' not in line, uppercase_lines)
for line in lines_without_dots:
print(line)
在行动中,在终端中:
thierry@amd:~$ ./test.py
My first line
--> MY FIRST LINE
goes through the pipeline
--> GOES THROUGH THE PIPELINE
but not this one, filtered because of the dot.
This last one will go through
--> THIS LAST ONE WILL GO THROUGH
只是,在哪里
地图
stdin
:
import sys
uppercase_lines = map(lambda line: line.upper(), sys.stdin)
for line in uppercase_lines:
print(line)
在行动中:
thierry@amd:~$ ./test2.py
this line will turn
THIS LINE WILL TURN
to uppercase
TO UPPERCASE