从字符串列表实例化两个2D numpy数组 [英] Instantiate two 2D numpy arrays from a list of strings

查看:50
本文介绍了从字符串列表实例化两个2D numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式的行列表:

I have a list of lines in the form:

"a, b, c, d, e ... z,"

第一个x需要在一个2D数组中保存为一行,其余的行在另一个2D数组中保存为一行.

Where the first x need to be saved as a row in one 2D array and the rest of the line saved as a row in another 2D array.

现在,如果这是C/C ++或Java,这将很容易,我可以在几秒钟内完成.但是我还没有100%习惯于python.但例如给出一个像这样的数组:

Now if this was in C/C++ or Java it would be easy and I could do it in a few seconds. But I haven't got 100% used to python yet. But given for example an array like:

['1, 2, 4, 5,', '2, 3, 6, 3,', '1, 1, 7, 6']

并被告知必须将第一个数组的列中的2个拆分为第二个数组中的两个,我如何将列表转换为以下两个numpy数组:

and being told that they have to be split 2 of the columns in the first array two in the second array how would I turn that list into the following two numpy arrays:

[[1, 2]
 [2, 3]
 [1, 1]]

和:

[[4, 5]
 [6, 3]
 [7, 6]]

出于我自己的理解,为什么我不能/不应该遍历嵌套的for循环中的每个元素并逐个覆盖它们.我不明白为什么当我尝试值不匹配所复制的内容.

Also for my own understanding why is it I can't/shouldn't go over each element in a nested for loop and overwrite them one by one. I don't understand why when I attempt that the values don't match what is copied.

作为参考,我尝试了以下代码:

For reference the code I tried:

    self.inputs=np.zeros((len(data), self.numIn))
    self.outputs=np.zeros((len(data),self.numOut))
    lineIndex=0
    for line in data:
        d=line.split(',')

        for i in range(self.numIn):
            self.inputs[lineIndex][i]=d[i]
            print d[i],
            self.inputs.index()
        for j in range(self.numOut):
            self.inputs[lineIndex][j]=d[self.numIn+j]
        lineIndex+=1

我想在python/numpy中创建一个包含所有值的numpy数组,然后将其拆分为两个单独的数组可能会更容易.如果这更容易帮助您,将不胜感激. (我建议的解决方案有多好!:P)

I suppose it may be easier in python/numpy to create one numpy array with all the values then split it into two separate arrays. If this is easier help with doing that would be appreciated. (How nice am I suggesting possible solutions! :P )

推荐答案

我同意这最后一点:

我想在python/numpy中创建一个包含所有值的numpy数组,然后将其拆分为两个单独的数组可能会更容易.如果这更容易帮助您,将不胜感激. (我建议的解决方案有多好!:P)

I suppose it may be easier in python/numpy to create one numpy array with all the values then split it into two separate arrays. If this is easier help with doing that would be appreciated. (How nice am I suggesting possible solutions! :P )

您可以(以删除尾部逗号),然后拆分(分为单个字符列表)中的每个字符串列表理解以获取行列表

You can strip (to remove trailing comma) and split (to break into list of single characters) each string in a list comprehension to get a list of rows

a = ['1, 2, 4, 5,', '2, 3, 6, 3,', '1, 1, 7, 6']
rows = [l.rstrip(',').split(',') for l in a]
rows
#[['1', ' 2', ' 4', ' 5'], ['2', ' 3', ' 6', ' 3'], ['1', ' 1', ' 7', ' 6']]

然后将其转换为整数数组:

Then convert it to an array of integers:

arr = np.array(rows, int)

arr
#array([[1, 2, 4, 5],
#       [2, 3, 6, 3],
#       [1, 1, 7, 6]])

得到两半:

arr[:, :2] # first two columns
#array([[1, 2],
#       [2, 3],
#       [1, 1]])

arr[:, -2:] # last two columns
#array([[4, 5],
#       [6, 3],
#       [7, 6]])

或者,返回两个数组:

a, b = np.split(arr, arr.shape[1]/2, axis=1)

这篇关于从字符串列表实例化两个2D numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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