[英]Reading space-separated integers efficiently
有沒有更有效的方法可以做到這一點?
>>> input_list = list(map(int, input().split()))
13 4 56 75 22 3
>>> input_list
[13, 4, 56, 75, 22, 3]
從您的評論中,我收集到您擔心內存使用情況,並且想要避免創建臨時列表。 不幸的是,Python沒有string.split(AFAIK)的迭代器版本,但是您可以使用re.finditer:
[int(match.group(0)) for match in re.finditer(r'\w+', input())]
但是,除非您的輸入長了幾兆字節,否則實際上不必擔心內存並使這樣的代碼復雜化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.