简体   繁体   中英

Reading space-separated integers efficiently

Is there a more efficient way to do this?

>>> input_list = list(map(int, input().split()))
13 4 56 75 22 3
>>> input_list
[13, 4, 56, 75, 22, 3]

From your comment I gather you are concerned about memory use and you want to avoid creating a temporary list. Unfortunately Python doesn't have an iterator version of string.split (AFAIK), but you can use re.finditer:

[int(match.group(0)) for match in re.finditer(r'\w+', input())]

But unless your input is many megabytes long there's really no need to worry about memory and complicate your code like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM