[英]Converting a list to a tuple of lists
I have a numpy ndarray which needs to be converted to a tuple of lists for multiprocessor.Pool to operate on them. 我有一个numpy ndarray,需要将其转换为多处理器列表的元组。池才能对其进行操作。 I have converted this ndarray to a list shown here:
我已经将此ndarray转换为此处显示的列表:
file_list = ['File_01', 'File_02', 'File_03']
In order to feed this list of files as an argument I believe I need this list to be a tuple of lists shown here: 为了提供此文件列表作为参数,我相信我需要此列表成为此处显示的列表的元组:
tuple_of_file_names = (['File_01'], ['File_02'], ['File_03'])
I am interested in how to convert this list of file names to a tuple of lists of file names. 我对如何将此文件名列表转换为文件名列表元组感兴趣。
Well if you originally had an ndarray
, the simplest thing you can do is add another axis to it using np.newaxis
/ None
and then obtain a nested list using tolist()
. 好吧,如果您最初有一个
ndarray
,那么您可以做的最简单的事情是使用np.newaxis
/ None
np.newaxis
添加另一个轴,然后使用tolist()
获得一个嵌套列表。
Here's an example: 这是一个例子:
x = np.array([1,2,3])
x[:, np.newaxis].tolist()
# [[1], [2], [3]]
Note: If you want the resulting list as a tuple simply call the tuple
constructor, as tuple(nested_list)
注意:如果要将结果列表作为元组,只需调用
tuple
构造函数,即tuple(nested_list)
简单的理解就能胜任:
tuple_of_file_names = tuple([name] for name in file_list)
Use map
+ lambda
and tuple()
to convert to tuple: 使用
map
+ lambda
和tuple()
转换为元组:
file_list = ['File_01', 'File_02', 'File_03']
tuple_of_file_names = tuple(map(lambda x: [x], file_list))
# (['File_01'], ['File_02'], ['File_03'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.