简体   繁体   中英

Combine two lists of lists - bitwise

I have two lists of lists-

dates = [['22/08/15'], ['24/08/15', '0900-1045', '1045-1100', '1100-1200', '1300-1430', '1430-1450', '1450-1550'],..........

data1 = [['Tuesday'].['Thursday','Room 5', 'Room 1', 'Room 1' ,'Room 2', 'Room 3', 'Room 5'],........

The sub-lists of each list are the same sizes but the lengths vary, ie the lengths of the sublists vary but the lists dates and data are symmetrical.

I want to combine the two "bitwise" giving -

[['22/08/15 Tuesday'], ['24/08/15 Thursday', '0900-1045 Room 5', '1045-1100 Room 1',.....

I have managed to do it with a very busy nested loop but it seems very complicated so I think there must be a better way.

Following is what I tried so far:

    x = 0
    print dates
    print data1
    while x < len(dates):
        b = 0
        print b
        while b < len(dates[x]):
            print dates[x][b]
            print data1[x][b]
            result = dates[x][b] + ' ' + data1[x][b]
            data2.append(result)  
            b = b + 1
        x = x + 1

What you want is like:

[[' '.join(p) for p in zip(d, c)] for d, c in zip(dates, comments)]

Here is it tested with the given data:

>> dates = [['22/08/15'], ['24/08/15', '0900-1045', '1045-1100', '1100-1200', '1300-1430', '1430-1450', '1450-1550']]
>> comments = [['Tuesday'], ['Thursday','Room 5', 'Room 1', 'Room 1' ,'Room 2', 'Room 3', 'Room 5']]
>> [[' '.join(p) for p in zip(d, c)] for d, c in zip(dates, comments)]
[['22/08/15 Tuesday'], ['24/08/15 Thursday', '0900-1045 Room 5', '1045-1100 Room 1', '1100-1200 Room 1', '1300-1430 Room 2', '1430-1450 Room 3', '1450-1550 Room 5']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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