且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

许多柜台的联盟

更新时间:2023-12-01 16:58:28

好的,Python程序员什么时候做的?害怕容易循环?

Goodness, when did Python programmers become afraid of easy loops? LOL.

result = Counter()
for c in counters:
    result |= c

在现实生活中,确实没有奖品可以将内容压缩成理论上尽可能少的字符。好吧,是的,Perl中有,但Python中没有;-)

There really aren't prizes in real life for squashing things into as few characters as theoretically possible. Well, ya, there are in Perl, but not in Python ;-)

稍后:根据user2357112的评论,从Python 3.3开始,以上代码将就地执行合并成结果。也就是说,结果是真正可重用的,可能在每次迭代中都变得更大。

Later: pursuant to user2357112's comment, starting with Python 3.3 the code above will do "in place" unions into result. That is, result is truly reused, possibly growing larger on each iteration.

任何

counters[0] | counters[1] | counters[2] | ...

相反,当下一个部分结果是计算的。这可能会或可能不会慢很多。

instead, the entire partial result so far keeps getting thrown away when the next partial result is computed. That may - or may not - be a lot slower.