结合两个列表列表 - 按位

时间:2016-03-06 17:27:10

标签: python regex

我有两个清单列表 -

  

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',&#39 ; 1号房间' ,' Room 2',' Room 3',' Room 5'],........

每个列表的子列表大小相同但长度不同,即子列表的长度不同,但列表日期和数据是对称的。

我想把两个"按位"给予 -

  

[[' 22/08/15 Tuesday'],[' 24/08/15 Thursday',' 0900-1045 Room 5',&# 39; 1045-1100房间1',.....

我已经成功地使用了一个非常繁忙的嵌套循环,但它似乎非常复杂,所以我认为必须有更好的方法。

以下是我到目前为止所做的尝试:

    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

1 个答案:

答案 0 :(得分:0)

你想要的是:

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

这是用给定数据测试的:

>> 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']]