如果我有一个形状如下的python列表:
T = [
['07,07,2012 22:10', ['people','drama','melody','bun']],
['08,07,2012 21:04', ['queen','group']],
['08,07,2012 21:23', ['printing','market','shopping']],
['08,07,2012 21:04', ['people','bun']],
['08,11,2012 11:14', ['kangaroo']]
]
我需要的是将此列表转换为以下格式:
T =[
['07,07,2012 22:10', 'people'],
['07,07,2012 22:10', 'drama'],
['07,07,2012 22:10', 'melody'],
['07,07,2012 22:10', 'bun'],
['08,07,2012 21:04', 'queen'],
['08,07,2012 21:04', 'group'],
['08,07,2012 21:23', 'printing'],
['08,07,2012 21:23', 'market'],
['08,07,2012 21:23', 'shopping'],
['08,07,2012 21:04', 'people'],
['08,07,2012 21:04', 'bun'],
[''08,11,2012 11:14'', 'kangaroo']
]
即。对于具有大于1的第一子元素的长度的每个元素(在原始列表T中),分割第一子元素(a[1] for a in T if len(a[1] > 1)
)并将其作为具有相同时间戳的另一列表附加。也许我的话缺乏解释,但上面的例子肯定是在解释我需要做什么。任何帮助将不胜感激。
答案 0 :(得分:5)
使用列表理解:
[(timestamp, item) for timestamp, items in T for item in items]
这将为您提供元组列表,这可能适用于此处。但您可以修改它以获取列表列表:
[[timestamp, item] for timestamp, items in T for item in items]
答案 1 :(得分:1)
在T:
上试试
def process(t):
new = []
for i in t:
for j in i[1]:
new.append([i[0], j])
return new