Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字符串列表到整数数组_Python_Arrays_Python 3.x_Numpy - Fatal编程技术网

Python 字符串列表到整数数组

Python 字符串列表到整数数组,python,arrays,python-3.x,numpy,Python,Arrays,Python 3.x,Numpy,从字符串列表中,例如: example_list = ['010','101'] example_array = np.array([[0,1,0],[1,0,1]]) 我需要得到一个整数数组,其中每一行都是字符串中的每一个,每一列中的每一个字符,如下所示: example_list = ['010','101'] example_array = np.array([[0,1,0],[1,0,1]]) 我已尝试使用此代码,但不起作用: example_array = np.empty([

从字符串列表中,例如:

example_list = ['010','101']
example_array = np.array([[0,1,0],[1,0,1]])
我需要得到一个整数数组,其中每一行都是字符串中的每一个,每一列中的每一个字符,如下所示:

example_list = ['010','101']
example_array = np.array([[0,1,0],[1,0,1]])
我已尝试使用此代码,但不起作用:

example_array = np.empty([2,3],dtype=int)    
i = 0 ; j = 0

for string in example_list:
    for bit in string:
        example_array[i,j] = int(bit)
        j+=1
    i+=1
有人能帮我吗?我正在使用Python 3.6

提前感谢您的帮助

您可以使用map执行此操作:

外部lambda对示例_list中的每个项执行listx操作。例如,'010'=>['0'、'1'、'0']。内部lambda将单个字符结果从listx转换为整数。例如,['0'、'1'、'0']=>0,1,0]。

您可以使用map执行此操作:


外部lambda对示例_list中的每个项执行listx操作。例如,'010'=>['0'、'1'、'0']。内部lambda将单个字符结果从listx转换为整数。例如,['0'、'1'、'0']=>[0,1,0]。

如果所有字符串的长度都相同,这对于构建连续数组至关重要,那么使用视图可以有效地分隔字符

r = np.array(example_list)
r = r.view('<U1').reshape(*r.shape, -1).astype(int)

print(r)
array([[0, 1, 0],
       [1, 0, 1]])

如果所有字符串的长度都相同,这对于构建连续数组至关重要,然后使用视图有效地分隔字符

r = np.array(example_list)
r = r.view('<U1').reshape(*r.shape, -1).astype(int)

print(r)
array([[0, 1, 0],
       [1, 0, 1]])

最简单的方法是使用列表理解,因为它会自动为您生成输出列表,可以轻松地转换为numpy数组。您可以使用多个for循环来实现这一点,但随后您将无法创建列表、子列表并附加到它们。虽然不难,但代码在列表理解方面看起来更加优雅

试试这个:

newList = np.array([[int(b) for b in a] for a in example_list])
新列表现在如下所示:

>>> newList
 ... [[0, 1, 0], [1, 0, 1]]
注意:此时不需要调用map,尽管这确实有效

这是怎么回事?我们将逐项遍历原始字符串列表,然后遍历当前项中的每个字符。从功能上讲,这相当于

newList = []

for a in example_list:
    tmpList = []
    for b in a:
        tmpList.append(int(b))
    newList.append(tmpList)

newList = np.array(newList)

我个人认为,对于初学者来说,多重for循环更容易理解。然而,一旦你掌握了清单上的理解,你可能就不想回头了

最简单的方法是使用列表理解,因为它会自动为您生成输出列表,可以轻松地将其转换为numpy数组。您可以使用多个for循环来实现这一点,但随后您将无法创建列表、子列表并附加到它们。虽然不难,但代码在列表理解方面看起来更加优雅

试试这个:

newList = np.array([[int(b) for b in a] for a in example_list])
新列表现在如下所示:

>>> newList
 ... [[0, 1, 0], [1, 0, 1]]
注意:此时不需要调用map,尽管这确实有效

这是怎么回事?我们将逐项遍历原始字符串列表,然后遍历当前项中的每个字符。从功能上讲,这相当于

newList = []

for a in example_list:
    tmpList = []
    for b in a:
        tmpList.append(int(b))
    newList.append(tmpList)

newList = np.array(newList)

我个人认为,对于初学者来说,多重for循环更容易理解。然而,一旦你掌握了清单上的理解,你可能就不想回头了

所有的字符串都一样长吗?@coldspeed是的,在我的例子中,它们都一样长。非常感谢您的帮助。所有的字符串都一样长吗?@coldspeed是的,在我的情况下,它们都一样长。非常感谢你们的帮助,我以前从未用过地图。然而,我开始欣赏它的实用性和优雅。很好的应用这个内置在这里。谢谢你的例子。一个更有效的方法是r[:,None]。查看“u4'-48”,尽管它复杂、依赖Unicode且难以理解,但我以前从未使用过map。然而,我开始欣赏它的实用性和优雅。很好的应用这个内置在这里。感谢您提供的示例。更有效的方法是r[:,None]。查看“u4'-48”,尽管其复杂、依赖Unicode且难以理解。在3.x映射中,返回的是不同于2.x的iterable,因此您的解决方案不完全符合OP的需要。示例数组=[*maplambda x:[*maplambda y:inty,listx],示例列表如何。虽然这看起来过于复杂…哦,别忘了转换为数组:示例数组=np.arrayexample\u arrayIn 3.x map返回一个不同于2.x的iterable,因此您的解决方案对于OP的需要是不完整的。示例数组=[*maplambda x:[*maplambda y:inty,listx],示例列表。虽然这看起来过于复杂…哦,别忘了转换为数组:示例数组=np.arrayexample\u数组