Python 列表到数组转换以使用 ravel() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15868512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List to array conversion to use ravel() function
提问by user2229953
I have a list in python and I want to convert it to an array to be able to use ravel()function.
我在 python 中有一个列表,我想将它转换为一个数组以便能够使用ravel()函数。
采纳答案by A. Rodas
回答by Mayank Sharma
if variable b has a list then you can simply do the below:
如果变量 b 有一个列表,那么您可以简单地执行以下操作:
create a new variable "a" as: a=[]then assign the list to "a" as: a=b
创建一个新变量“a”为:a=[]然后将列表分配给“a”为:a=b
now "a" has all the components of list "b" in array.
现在“a”在数组中包含列表“b”的所有组件。
so you have successfully converted list to array.
所以你已经成功地将列表转换为数组。
回答by D_C
I wanted a way to do this without using an extra module. First turn list to string, then append to an array:
我想要一种不使用额外模块的方法。首先将列表转换为字符串,然后附加到数组:
dataset_list = ''.join(input_list)
dataset_array = []
for item in dataset_list.split(';'): # comma, or other
dataset_array.append(item)
回答by Uszkai Attila
create an int array and a list
创建一个 int 数组和一个列表
from array import array
listA = list(range(0,50))
for item in listA:
print(item)
arrayA = array("i", listA)
for item in arrayA:
print(item)
回答by Vinay
Use the following code:
使用以下代码:
import numpy as np
myArray=np.array([1,2,4]) #func used to convert [1,2,3] list into an array
print(myArray)
回答by Paul Panzer
If all you want is calling ravelon your (nested, I s'pose?) list, you can do that directly, numpywill do the casting for you:
如果你想要的只是调用ravel你的(嵌套的,我想?)列表,你可以直接numpy这样做,会为你做演员:
L = [[1,None,3],["The", "quick", object]]
np.ravel(L)
# array([1, None, 3, 'The', 'quick', <class 'object'>], dtype=object)
Also worth mentioning that you needn't go through numpyat all.
另外值得一提的是,你不必去通过numpy所有。

