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

4
  • 1
    think it's already merged. Commented Jul 17, 2015 at 5:14
  • 1
    Either that, or you want dataTest=sum(dataTest,[]), can't really tell. Commented Jul 17, 2015 at 5:17
  • Your result matrix is missing commas or some other syntax, so it's hard to tell what you actually are asking. Commented Jul 17, 2015 at 5:33
  • Working with lists of lists of lists gets very cumbersome, I'd look for an easier to use datatype if I were you. Please don't use the word "dimension" because it makes it seem like you're using multi-dimensional arrays (which lists of lists are not). Commented Jul 17, 2015 at 9:58

5 Answers 5

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]]
Sign up to request clarification or add additional context in comments.

7 Comments

I want somethong like this: [[1,2] in a row, and [2,3]] in another row
@akh isn't your data already like that ! do you want to iterate over list1 to list50 and append it to dataset
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 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
@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.
|
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')
    ]

Comments

0

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

dataTest=sum(dataTest,[])

2 Comments

Could you add sample input and output
Please don't recommend this. Using sum with lists has quadratic performance.
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']

3 Comments

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
Ah I see, the answer has been updated. Let me know if you're ok.
import * is a bad idea. Always import the exact thing you want to use.
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']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.