建立了一个文件‘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的最后一个单词)
把
[word for line in f for word in line.split()]
这行代码写成for循环就是:同样的道理,把
[word for word in line.split() for line in f]
写成for循环是:内层循环改变了外层循环中
line
的值,写代码的时候应该避免这种情况。你之所以觉得第二个列表解析的逻辑更清楚,是因为经常看到类似
[[word for word in line.split()] for line in f]
的代码,内层的列表被当成了外层列表的一个元素,两个for循环是处于不同层次的,跟你写的两层for循环嵌套是不一样的。