我正在尝试使用列表理解执行以下操作:
Input: [['hello ', 'world '],['foo ',' bar']]
Output: [['hello', 'world'], ['foo', 'bar']]
以下是在没有列表推导的情况下如何做到这一点:
a = [['hello ', 'world '],['foo ',' bar']]
b = []
for i in a:
temp = []
for j in i:
temp.append( j.strip() )
b.append( temp )
print(b)
#[['hello', 'world'], ['foo', 'bar']]
如何使用列表推导式来做到这一点?
最佳答案
a = [['hello ', 'world '],['foo ',' bar']]
b = [[s.strip() for s in l] for l in a]
print(b)
# [['hello', 'world'], ['foo', 'bar']]
关于python - 双 for 循环列表理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249350/