[英]What’s the most Pythonic way of handling different, mutually-exclusive function inputs?
[英]What is the most Pythonic way of handling standard input?
我已經開始進行編程競賽/挑戰賽,而且問題常常涉及從標准輸入中讀取內容。 我一直在做
import fileinput
inputLines = []
for line in fileinput.input():
inputLines.append(line)
然后,我可以執行需要對inputLines
進行的任何計算。 有沒有更Pythonic(即更好)的方式來做到這一點?
如果只想從stdin
讀取而不是從命令行中命名的任何文件中讀取,則不應使用fileinput
。
如果您想要一個包含stdin
中各行的列表,則:
import sys
inputLines = list(sys.stdin)
import sys
for line in sys.stdin:
print "The line was", line
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.