python 读入多行数据
下面为大家分享一篇python 读入多行数据的实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
一、前言
本文主要使用python 的raw_input() 函数读入多行不定长的数据,输入结束的标志就是不输入数字情况下直接回车,并填充特定的数作为二维矩阵
二、代码
def get2DlistData(): res = [] inputLine = raw_input() #以字符串的形式读入一行 #如果不为空字符串作后续读入 while inputLine != '': listLine = inputLine.split(' ') #以空格划分就是序列的形式了 listLine = [int(e) for e in listLine ] #将序列里的数由字符串变为int类型 res.append( listLine ) inputLine = raw_input() return res def get2DMatData( inList , val ): ''' 输入:inList为不等长的二维数组,val为需填充的值 输出:np.array的形式输出填补完的二维矩阵 ''' maxCols = 0 #获得最大长度的子序列 for i in range( len( inList ) ): lenI = len( inList[i] ) if lenI > maxCols: maxCols= lenI #对每个子序列作填充 for i in range( len( inList ) ): num = len( inList[i] ) while(num <p></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><p><span style="color: #ff0000"><strong>三、运行结果</strong></span></p><p><img alt="" src="https://img.php.cn/upload/article/000/153/291/60da956730dbb123d34b526bf42cf465-0.jpg"></p><p>