使用理解嵌套列表?

时间:2012-10-31 23:54:52

标签: python

我有一个清单:

['EFJAJCOWSS', 'SDGKSRFDFF', 'ASRJDUSKLK', 'HEANDNDJWA', 'ANSDNCNEOP', 'PMSNFHHEJE', 'JEPQLYNXDL']

从此列表中我如何创建子列表:

[['EFJAJCOWSS'], ['SDGKSRFDFF'], ['ASRJDUSKLK'], ['HEANDNDJWA'], ['ANSDNCNEOP'], ['PMSNFHHEJE'], ['JEPQLYNXDL']]

在Python中使用列表推导?

1 个答案:

答案 0 :(得分:1)

In [45]: lis=['EFJAJCOWSS', 'SDGKSRFDFF', 'ASRJDUSKLK', 'HEANDNDJWA', 'ANSDNCNEOP', 'PMSNFHHEJE', 'JEPQLYNXDL']

In [46]: [[x] for x in lis]
Out[46]: 
[['EFJAJCOWSS'],
 ['SDGKSRFDFF'],
 ['ASRJDUSKLK'],
 ['HEANDNDJWA'],
 ['ANSDNCNEOP'],
 ['PMSNFHHEJE'],
 ['JEPQLYNXDL']]

或使用map()

In [47]: map(lambda x:[x],lis)
Out[47]: 
[['EFJAJCOWSS'],
 ['SDGKSRFDFF'],
 ['ASRJDUSKLK'],
 ['HEANDNDJWA'],
 ['ANSDNCNEOP'],
 ['PMSNFHHEJE'],
 ['JEPQLYNXDL']]