简体   繁体   English

如何从两个列表中获取一个列表

[英]How to get a list out of two lists

I have two lists (both are already ordered correctly and contain 180.000 items each).我有两个列表(都已正确订购,每个包含 180.000 项)。 The first one contains the filenames.第一个包含文件名。

filenames[0]
s01_l01/1000_1.png

The second list contains the labels of the files.第二个列表包含文件的标签。

labels[0]
'1BK 7475'

How do I now get a list, which looks like this: [[filename_1, label_1], [filename_2, label_2],..., [filename_n, label_n]]?我现在如何获得一个列表,如下所示:[[filename_1, label_1], [filename_2, label_2],..., [filename_n, label_n]]?

list_of_filenames_labels[0]
[s01_l01/1000_1.png, '1BK 7475']

Thanks a lot!非常感谢!

You can do that with a simple map with lambda function您可以使用简单的 map 和 lambda function 来做到这一点

a = ['1','2','3']
b = ['a','b','c']
c = list(map(lambda x, y: [x,y], a, b))

The output for c would be c 的 output 将是

c = [['1', 'a'], ['2', 'b'], ['3', 'c']]

The output you specified is similar to the output of zip , except that it is a list of lists rather than a list of tuples.您指定的 output 类似于 zip 的zip ,除了它是列表列表而不是元组列表。 This would convert them to lists:这会将它们转换为列表:

[list(t) for t in zip(filenames, labels)]

Here:这里:

list_of_filenames_labels = [[filename,label] for filename,label in zip(filenames,labels)]

if both list have same length, you can use enumerate function如果两个列表的长度相同,则可以使用enumerate function

list_of_filenames_labels = [[filename, labels[i]] for i, filename in enumerate(filenames)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM