如何阅读整数从python3输入的单行数组 [英] How to read an array of integers from single line of input in python3

查看:566
本文介绍了如何阅读整数从python3输入的单行数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读整数从python3输入的单行数组。
例如:阅读这个数组变量/列表

  1 3 5 7 9

什么我都试过


  1. ARR = input.split('')但这并不将它们转换为整数。它创建的字符串数组


  2. ARR = input.split('')

    对于我,VAL在历数(ARR):常用3 [I] = INT(VAL)


第二一个是为我工作。但我在寻找一个优雅(单线)解决方案。


解决方案

使用地图

  ARR =列表(图(INT,input.split()))

只是添加,在Python 2.x的,你不需要调用的list(),因为地图()已返回列表,但在Python 3.x的许多流程该迭代iterables返回迭代器自己

I want to read an array of integers from single line of input in python3. For example: Read this array to a variable/list

1 3 5 7 9

What I have tried

  1. arr = input.split(' ') But this does not convert them to integers. It creates array of strings

  2. arr = input.split(' ')

    for i,val in enumerate(arr): arr[i] = int(val)

2nd one is working for me. But I am looking for an elegant(Single line) solution.

解决方案

Use map:

arr = list(map(int, input.split()))

Just adding, in Python 2.x you don't need the to call list(), since map() already returns a list, but in Python 3.x "many processes that iterate over iterables return iterators themselves".

这篇关于如何阅读整数从python3输入的单行数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆