2015-02-10 103 views
-1

當前結果返回與Python返回值

[['jackson', ['21', '22'], ['20','24']], ['barkley', ['30', '32'], ['33', '34']]] 

我要來分隔值,使

a = jackson, barkley 
    b = (jackson, 21), (jackson, 22), (jackson, 23), (jackson 24), (barkley, 30)..etc 

什麼是最好的方法是什麼?謝謝。

+3

所以你嘗試過這麼遠嗎? – dragon2fly 2015-02-10 00:44:36

+0

@mgilson是的。對不起,錯字.. – dangerous93 2015-02-10 00:47:04

+0

提示:使用for循環。 – l00kitsjake 2015-02-10 00:54:48

回答

4
>>> d = [['jackson', ['21', '22'], ['20','24']], ['barkley', ['30', '32'], ['33', '34']]] 
>>> [(i[0],z) for i in d for j in i[1:] for z in j] 
[('jackson', '21'), ('jackson', '22'), ('jackson', '20'), ('jackson', '24'), ('barkley', '30'), ('barkley', '32'), ('barkley', '33'), ('barkley', '34')] 

你只是遍歷它,然後遍歷子列表... OK沒問題:P

+1

我愛列表解析!它看起來很精英。 – ozgur 2015-02-10 00:55:34