欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python列表推导式之嵌套的推导解析

程序员文章站 2024-01-06 13:41:28
...

直接看代码:

Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ls = ['尹增宝','陈金涵','林祖泉']
>>> ls
['尹增宝', '陈金涵', '林祖泉']
>>> info = [word for word in name for name in ls]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>> info = [word for name in ls for word in name]
>>> info
['尹', '增', '宝', '陈', '金', '涵', '林', '祖', '泉']
>>>
>>>

上一篇:

下一篇: