我有两个列表列表-
日期 = [['22/08/15'每个列表的子列表大小相同,但长度不同], ['24/08/15', '0900-1045', '1045-1100', '1100-1200', '1300-1430', '1430-1450', '1450-1550'],..........
data1 = [['星期二'].['星期四","5号房间","1号房间","1号房间","2号房间","3号房间","5号房间"],........
,即子列表的长度不同,但列表日期和数据是对称的。
我想结合两个"按位"给出——
[['22/08/15 星期二'], ['24/08/15 星期四', '0900-1045 房间 5', '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
你想要的是:
[[' '.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']]