[Python] 列表解析的嵌套的逻辑

 绝恋love2502860291 发布于 2022-10-30 06:25

建立了一个文件‘ttt’,内容有7行,如下:

Making the Aston Martin DB11 our own

There's quite a lot of excitement around these parts about the new Aston Martin DB11. The look is certainly polarizing, but we'd also argue it's much more exciting than the DB9. And most importantly, it's got power. A lot of power.

That makes it a vehicle we're very much looking forward to driving. But since that date is still quite a long way off in the distance, we're stuck doing nothing more than playing with the newly launched configurator. Like the majority of exotic car configurators, the options are extensive and the pricing isn't listed, but we were able to gain some insights based on how the AB staff designed its cars.

For example, of the six editors that turned in cars, all but one took advantage of Aston's blacked-out roof option. With a few clicks, the DB11's floating roof is gone. And most of us went with more low-key color options. While the Autoblog staff is hardly representative of the DB11's future customers, it will be interesting to see how these selections, especially the floating-roof delete, play out when actual orders start rolling in. And if you want to go design your perfect DB11, check out the configurator.

python脚本如下:

>>> a=r"open('ttt','r')"
>>> f=eval(a)
>>> print [word for line in f for word in line.split()]
['Making', 'the', 'Aston', 'Martin', 'DB11', 'our', 'own', "There's", 'quite', 'a', 'lot', 'of', 'excitement', 'around', 'these', 'parts', 'about', 'the', 'new', 'Aston', 'Martin', 'DB11.', 'The', 'look', 'is', 'certainly', 'polarizing,', 'but', "we'd", 'also', 'argue', "it's", 'much', 'more', 'exciting', 'than', 'the', 'DB9.', 'And', 'most', 'importantly,', "it's", 'got', 'power.', 'A', 'lot', 'of', 'power.', 'That', 'makes', 'it', 'a', 'vehicle', "we're", 'very', 'much', 'looking', 'forward', 'to', 'driving.', 'But', 'since', 'that', 'date', 'is', 'still', 'quite', 'a', 'long', 'way', 'off', 'in', 'the', 'distance,', "we're", 'stuck', 'doing', 'nothing', 'more', 'than', 'playing', 'with', 'the', 'newly', 'launched', 'configurator.', 'Like', 'the', 'majority', 'of', 'exotic', 'car', 'configurators,', 'the', 'options', 'are', 'extensive', 'and', 'the', 'pricing', "isn't", 'listed,', 'but', 'we', 'were', 'able', 'to', 'gain', 'some', 'insights', 'based', 'on', 'how', 'the', 'AB', 'staff', 'designed', 'its', 'cars.', 'For', 'example,', 'of', 'the', 'six', 'editors', 'that', 'turned', 'in', 'cars,', 'all', 'but', 'one', 'took', 'advantage', 'of', "Aston's", 'blacked-out', 'roof', 'option.', 'With', 'a', 'few', 'clicks,', 'the', "DB11's", 'floating', 'roof', 'is', 'gone.', 'And', 'most', 'of', 'us', 'went', 'with', 'more', 'low-key', 'color', 'options.', 'While', 'the', 'Autoblog', 'staff', 'is', 'hardly', 'representative', 'of', 'the', "DB11's", 'future', 'customers,', 'it', 'will', 'be', 'interesting', 'to', 'see', 'how', 'these', 'selections,', 'especially', 'the', 'floating-roof', 'delete,', 'play', 'out', 'when', 'actual', 'orders', 'start', 'rolling', 'in.', 'And', 'if', 'you', 'want', 'to', 'go', 'design', 'your', 'perfect', 'DB11,', 'check', 'out', 'the', 'configurator.']

>>> f.close()
>>> f=eval(a)
>>> print [word for word in line.split() for line in f]
['For', 'For', 'For', 'For', 'For', 'For', 'For']
>>> word
'configurator.'

我觉得第二个print语句里的列表解析式里的逻辑更清楚一些,但是得到的确是7次重复了最后一个line的首个单词“For”,这是什么逻辑造成的?
1、程序是怎么计算出来要重复7次的?
2、为什么重复的是’For‘而不是'configurator.'?('configurator.'是最后一个line的最后一个单词)

3 个回答
  • 我就看看~!!!!!!!!!!!!!!!!!!!!!

    2022-10-31 21:17 回答
  • 在运行第一段程序的时候,已经给word和line赋值,而在运行第二段之前没有释放掉word和line这两个变量,所以导致了第二段程序可以运行还不报错。

    >>> a=r"open('ttt','r')"
    >>> f=eval(a)
    >>> print [word for line in f for word in line.split()]
    ['Making', 'the', 'Aston', 'Martin', 'DB11', 'our', 'own', "There's", 'quite', 'a', 'lot', 'of', 'excitement', 'around', 'these', 'parts', 'about', 'the', 'new', 'Aston', 'Martin', 'DB11.', 'The', 'look', 'is', 'certainly', 'polarizing,', 'but', "we'd", 'also', 'argue', "it's", 'much', 'more', 'exciting', 'than', 'the', 'DB9.', 'And', 'most', 'importantly,', "it's", 'got', 'power.', 'A', 'lot', 'of', 'power.', 'That', 'makes', 'it', 'a', 'vehicle', "we're", 'very', 'much', 'looking', 'forward', 'to', 'driving.', 'But', 'since', 'that', 'date', 'is', 'still', 'quite', 'a', 'long', 'way', 'off', 'in', 'the', 'distance,', "we're", 'stuck', 'doing', 'nothing', 'more', 'than', 'playing', 'with', 'the', 'newly', 'launched', 'configurator.', 'Like', 'the', 'majority', 'of', 'exotic', 'car', 'configurators,', 'the', 'options', 'are', 'extensive', 'and', 'the', 'pricing', "isn't", 'listed,', 'but', 'we', 'were', 'able', 'to', 'gain', 'some', 'insights', 'based', 'on', 'how', 'the', 'AB', 'staff', 'designed', 'its', 'cars.', 'For', 'example,', 'of', 'the', 'six', 'editors', 'that', 'turned', 'in', 'cars,', 'all', 'but', 'one', 'took', 'advantage', 'of', "Aston's", 'blacked-out', 'roof', 'option.', 'With', 'a', 'few', 'clicks,', 'the', "DB11's", 'floating', 'roof', 'is', 'gone.', 'And', 'most', 'of', 'us', 'went', 'with', 'more', 'low-key', 'color', 'options.', 'While', 'the', 'Autoblog', 'staff', 'is', 'hardly', 'representative', 'of', 'the', "DB11's", 'future', 'customers,', 'it', 'will', 'be', 'interesting', 'to', 'see', 'how', 'these', 'selections,', 'especially', 'the', 'floating-roof', 'delete,', 'play', 'out', 'when', 'actual', 'orders', 'start', 'rolling', 'in.', 'And', 'if', 'you', 'want', 'to', 'go', 'design', 'your', 'perfect', 'DB11,', 'check', 'out', 'the', 'configurator.']
    >>> line
    "For example, of the six editors that turned in cars, all but one took advantage of Aston's blacked-out roof option. With a few clicks, the DB11's floating roof is gone. And most of us went with more low-key color options. While the Autoblog staff is hardly representative of the DB11's future customers, it will be interesting to see how these selections, especially the floating-roof delete, play out when actual orders start rolling in. And if you want to go design your perfect DB11, check out the configurator."
    >>> word
    'configurator.'

    这个时候的line和word都已经被赋值。
    继续运行[word for word in line.split() for line in f],相当于:

    >>> f.close()
    >>> f=eval(a)
    >>>for word in line.split():     #这时word被赋值为line.split()列表的第一个元素'For'
    ...    for line in f:            #line in f 会循环7次,导致:
    ...        print word            #打印word 7次,这个时候word的值一直都是'For',所以会出现7次'For'
    

    当内部循环for line in f:完成第一轮后,外部循环for word in line.split():启动第二轮。在后台,迭代器会判断迭代对象是否发生了变化,如果有变化则终止循环。如果迭代器是在for语句内,则不会报错。

    2022-10-31 21:17 回答
  • [word for line in f for word in line.split()]这行代码写成for循环就是:

    for line in f:
        for word in line.split():
            print word

    同样的道理,把[word for word in line.split() for line in f]写成for循环是:

    for word in line.split():
        for line in f:
            print word

    内层循环改变了外层循环中line的值,写代码的时候应该避免这种情况。

    你之所以觉得第二个列表解析的逻辑更清楚,是因为经常看到类似[[word for word in line.split()] for line in f]的代码,内层的列表被当成了外层列表的一个元素,两个for循环是处于不同层次的,跟你写的两层for循环嵌套是不一样的。

    2022-10-31 21:17 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有