0

I already read this, this, and this, but no solution.

I have a list of 50 lists like this:

dataTest=[List1,List2,List3,...,List50]

Each of those lists is a list of images along with labels. I mean the first dimension of List1 is consists of some images, and the second dimension of the List1 is their corresponding labels. like this:

List1=[Images,Labels]

For example, List1 is a list with 700 images with label 0. List2 is a list with 1000 images with label 1, ... Hence, we have a list of 50 lists in which each individual list comes with size 2.

I want to merge all list together in order to get the following matrix. I mean an array like this:

dataTest=[Images of List1,2,3,...,50  , Labels of List1,2,3,...,50]

So, we would have a list with two size. The first dimension is all images from all lists, and the second dimension is the labels of all images

Community
  • 1
  • 1
akh
  • 13
  • 4

5 Answers5

1

Is this what you want:

a=[[1,2],[2,3]]
[v for d in a for v in d]
[1, 2, 2, 3]

Changed after OP's edit:

a=[[2,3],[2,3]]
c=[]
c.append([v[0] for v in a])
c.append([v[1] for v in a])
print c
[[2, 2], [3, 3]]
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • I want somethong like this: [[1,2] in a row, and [2,3]] in another row – akh Jul 17 '15 at 05:27
  • @akh isn't your data already like that ! do you want to iterate over list1 to list50 and append it to dataset – The6thSense Jul 17 '15 at 05:28
  • I have 10000 images that I reshape them into a row. They are in different labels. I divided them based on their labels, and called List1,2, ...,50. Now I want to merge them sequentially. I mean, I want an array of all of them. – akh Jul 17 '15 at 05:34
  • @akh it is really confusing of what you are asking can you provide same input and sample output which is more explanatory then the previous one – The6thSense Jul 17 '15 at 05:35
  • @Vignesh, no. I have different lists of images. I want to merge all of them into one list. – akh Jul 17 '15 at 06:12
  • @akh the first list consists of only list of images the second is label what more do you want – The6thSense Jul 17 '15 at 06:18
  • 1
    @VigneshKalai, I can use 'zip(*)' to find what you say. It provides a list of the lists. What I want is to separate all lists, and then merge together. May be an image from a list come after that an image from another list. – akh Jul 17 '15 at 06:32
1

If I got it right, then the data "List1=[Images,Labels]" has the structure that Images is a list of images and Labels is a list of labels? If that is true, the code below merges them and generates a resulting list which holds a list (or better a tuple, but that can of course be converted) of all images and a list of all labels:

    # generate sample data
    l1 = [['IMG_1', 'IMG_2', 'IMG_3', 'IMG_4'],['IMG_LABEL_1', 'IMG_LABEL_2', 'IMG_LABEL_3', 'IMG_LABEL_4']]
    l2 = [['IMG_11', 'IMG_12', 'IMG_13', 'IMG_14'],['IMG_LABEL_11', 'IMG_LABEL_12', 'IMG_LABEL_13', 'IMG_LABEL_14']]
    l3 = [['IMG_21', 'IMG_22', 'IMG_23', 'IMG_24'],['IMG_LABEL_21', 'IMG_LABEL_22', 'IMG_LABEL_23', 'IMG_LABEL_24']]
    data = [l1, l2, l3]

    merged_list = []

    # merge the lists
    for source_list in data:
        merged_list += zip(source_list[0], source_list[1])

    # change its structure
    result_list = zip(*merged_list)

Results in:

    [
    ('IMG_1', 'IMG_2', 'IMG_3', 'IMG_4', 'IMG_11', 'IMG_12', 'IMG_13', 'IMG_14', 'IMG_21', 'IMG_22', 'IMG_23', 'IMG_24'), 
    ('IMG_LABEL_1', 'IMG_LABEL_2', 'IMG_LABEL_3', 'IMG_LABEL_4', 'IMG_LABEL_11', 'IMG_LABEL_12', 'IMG_LABEL_13', 'IMG_LABEL_14', 'IMG_LABEL_21', 'IMG_LABEL_22', 'IMG_LABEL_23', 'IMG_LABEL_24')
    ]
mstuebner
  • 406
  • 2
  • 15
0

You just need to flatten the list by the looks of things, you can do so with itertools

from itertools import*
dataTest=[['a',1],['a',2],['a',3]]
print [k for k in chain.from_iterable(k[0] for k in dataTest)]

Output

['a', 'a', 'a']
ajsp
  • 2,512
  • 22
  • 34
  • Actually I want to add all images from all lists together in an array. Foe example, if the number of images in all lists are 10000, I want to have an array with 10000 images – akh Jul 17 '15 at 05:25
  • Ah I see, the answer has been updated. Let me know if you're ok. – ajsp Jul 17 '15 at 05:30
  • `import *` is a bad idea. Always import the exact thing you want to use. – belteshazzar Jul 17 '15 at 05:58
0

If flattening a list is what you're trying to do, you can just sum the lists together.

dataTest=sum(dataTest,[])
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
0

I have created a dataTest hopefully in the same format as you have:

List1 = [["img11", "img12", "img13"], ["label11", "label12", "label13", "label14"]]
List2 = [["img21", "img22", "img23"], ["label21", "label22", "label23", "label24"]]
List3 = [["img31", "img32", "img33"], ["label31", "label32", "label33", "label34"]]

dataTest = [List1, List2, List3]

print "Input:", dataTest
print

dataTest2 = [[],[]]

for cur_list in dataTest:
    dataTest2[0].extend(cur_list[0])
    dataTest2[1].extend(cur_list[1])

print "Output:", dataTest2

This produces a list with 2 elements, the first containing all of the images, and the second containing all of the labels:

Input: [[['img11', 'img12', 'img13'], ['label11', 'label12', 'label13', 'label14']], [['img21', 'img22', 'img23'], ['label21', 'label22', 'label23', 'label24']], [['img31', 'img32', 'img33'], ['label31', 'label32', 'label33', 'label34']]]

Output: [['img11', 'img12', 'img13', 'img21', 'img22', 'img23', 'img31', 'img32', 'img33'], ['label11', 'label12', 'label13', 'label14', 'label21', 'label22', 'label23', 'label24', 'label31', 'label32', 'label33', 'label34']]
Martin Evans
  • 45,791
  • 17
  • 81
  • 97