2014-01-07 49 views
-1

我有一個列表內的列表,基本上試圖修改所有的情況下,我看到一個關鍵字。 例如:列表內的列表更改

test_out = [['I am monkey free.'], ['I explained my life free.']] 

A = "monkey free" 

我的目標是,在TEST_OUT列表中的每個實例,我看到A =「錢免費」,取代以「XXXXXX」

輸出應該然後是:

test_out = [['I am xxxxxx'], ['I explained my life free.']] 

任何想法將不勝感激。

謝謝。

+0

免費或猴免費?有一個區別:) – roippi

+0

大聲笑。猴先生! – BlackHat

+1

你有什麼嘗試? –

回答

1

這裏:

>>> test_out = [['I am monkey free.'], ['I explained my life free.']] 
>>> A = "monkey free" 
>>> replacement = "xxxxxx" 
>>> [[string.replace(A, replacement) for string in sublist] for sublist in test_out] 
[['I am xxxxxx.'], ['I explained my life free.']] 

難道你真的想你列出兩個嵌套深有關係嗎?

+0

我在開始的時候把它弄得很深,所以我馬上有點猶豫不決。我可能會在幾天內把它弄平。謝謝您的幫助。 – BlackHat

+0

@ user3116753夠公平的。但是,如果你想扁化列表,它很簡單:'new_list = [子列表中項目的test_out中的子列表的項目]''。 – senshin