2013-03-19 87 views
-7

我有一個這樣的名單:如何僅在Python中將此列表元組更改爲列表?

[[(u'[email protected]',)], [(u'[email protected]',)]] 

我想這個轉換爲:

[['[email protected]'], ['[email protected]']] 

我怎樣才能做到這一點?

又如: 輸入:[[(u'hello',), (u'hello',)], [(u'hello',)]] 應該返回[['[email protected]', '[email protected]'], ['[email protected]']]

+0

我爲什麼downvoted這個問題:http://meta.stackexchange.com/a/149138/133242 – 2013-03-19 12:48:54

回答

0
test = [[(u'hello',), (u'hello',)], [(u'hello',)]] 
for i, part_i in enumerate(test): 
    for j, part_j in enumerate(part_i): 
     test[i][j] = str(part_j[0]) 

或者,如果你喜歡一個行版本:

test = [[(u'hello',), (u'hello',)], [(u'hello',)]] 
result = [[str(j[0]) for j in i] for i in test] 
0
new = [[j[0].encode('utf-8') for j in i] for i in old] 

UTF-8只是一個例子,你應該確保你得到的編碼權。

+0

還是一個unicode?如何刪除? – user1881957 2013-03-19 12:51:54

+0

不適用:[[(u'hello',),(u'hello',)],[(u'hello',)]]應該返回[['[email protected]',hello @ hello.com],['[email protected]']] – user1881957 2013-03-19 12:58:45

相關問題