2014-01-07 48 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