避免劣化的python代码 的争议

本文介绍Python中迭代器的高效使用方法,包括xrange与enumerate的区别、xrange与zip的不同应用场景、filter与itertools.ifilter的选择、imap与map的对比、生成器的使用以及groupby的应用等。
[size=large][color=red][b]1.xrange and enumerate[/b][/color][/size]

[b]enumerate[/b]:enumerate is useful for obtaining an indexed list
[b]xrange[/b]: generates the numbers in the range on demand. For looping, this is slightly faster than range() and more memory efficient.


根据性能比较还是xrange 好一点,如果数据量不大,用哪个都可以,哪个更符合要求您就可以使用哪个,而且enumerate和xrange同样使用的是next()方法,只是对返回数据的封装不同。

seq = [i for i in xrange(1000000)]
... n = datetime.datetime.now()
... for i in xrange(len(seq)):
... #print item, i
... pass
... print datetime.datetime.now() - n
0:00:00.066000
>>> seq = [i for i in xrange(1000000)]
... n = datetime.datetime.now()
... for i in xrange(len(seq)):
... #print item, i
... pass
... print datetime.datetime.now() - n
0:00:00.067000



print datetime.datetime.now() - n
0:00:00.142000
>>> seq = [i for i in xrange(1000000)]
... n = datetime.datetime.now()
... for i,item in enumerate(seq):
... #print item, i
... pass
... print datetime.datetime.now() - n
0:00:00.142000



[size=large][color=red][b]2.xrange and zip[/b][/color][/size]

[b]在这里zip和xrange的功能是不一样滴,不能做功能上的比较[/b]

劣化代码:
for i in xrange(len(seq1)):
foo(seq1[i], seq2[i])
推荐代码:
for i, j in zip(seq1, seq2)
foo(i, j)
更高效:
for i, j in itertools.izip(seq1, seq2):
foo(i, j)


这里是zip的定义:
[b]zip(...)[/b]
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The[b][color=red] returned list is truncated
in length to the length of the shortest argument sequence[/color][/b].
[color=red][b]如果用zip做代替结果可能会产生错误!,如下代码没有list中的6[/b]
[/color]

>>> zip([4,5,6],[2,3])
2: [(4, 2), (5, 3)]



[color=red]而且zip的性能也不是太理想:[/color]
import datetime
... seq1 = [i for i in xrange(1000000)]
... seq2 = [j for j in xrange(1000000)]
... n = datetime.datetime.now()
... for i, j in zip(seq1, seq2):
... pass
... print datetime.datetime.now() - n
0:00:01.713000


>>> import itertools
>>> import datetime
... seq1 = [i for i in xrange(1000000)]
... seq2 = [j for j in xrange(1000000)]
... n = datetime.datetime.now()
... for i, j in itertools.izip(seq1, seq2):
... pass
... print datetime.datetime.now() - n
0:00:00.159000



[size=large][color=red][b]3.filter[/b][/color][/size]
不过下面这个写法还是挺好的,可以提高复用性
劣化代码:
for i in seq:
if pred(i):
foo(i)
推荐代码:
for i in itertools.ifilter(pred, seq):
foo(i)



[size=large][color=red][b]4.imap[/b][/color][/size]
map and iterator.imap 也是有很好的复用性,
但是imap和map的定义不同:

itertools.imap(function, *iterables)
Make an iterator that computes the function using arguments from each of the iterables. If function is set to None, then imap() returns the arguments as a tuple. Like map() [b][color=red]but stops when the shortest iterable is exhausted[/color] [/b]instead of filling in None for shorter iterables. The reason for the difference is that infinite iterator arguments are typically an error for map() (because the output is fully evaluated) but represent a common and useful way of supplying arguments to imap()

两者的区别


for i in map(pow, (2,3,10), (5,2)):
... print i
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
for i in map(pow, (2,3,10), (5,2)):
TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'NoneType'



> for i in itertools.imap(pow, (2,3,10), (5,2)):
... print i
32
9



[size=large][color=red][b]5.Generators[/b][/color][/size]
Since Python 2.2, generators provide an elegant way to write simple and efficient
code for functions that return a list of elements. Based on the yield directive, they
allow you to pause a function and return an intermediate result. The function saves
its execution context and can be resumed later if necessary.
For example (this is the example provided in the PEP about iterators), the Fibonacci
series can be written with an iterator:
>>> def fibonacci():
... a, b = 0, 1
... while True:
... yield b
... a, b = b, a + b
...
>>> fib = fibonacci()
>>> fib.next()
1
>>> fib.next()
1
>>> fib.next()
2
>>> [fib.next() for i in range(10)]
[3, 5, 8, 13, 21, 34, 55, 89, 144, 233]



>>> def my_generator():
... try:
... yield 'something'
... except ValueError:
... yield 'dealing with the exception'
... finally:
... print "ok let's clean"
>>> m = my_generator
>>> m = my_generator()
>>> m.next()
26: 'something'
>>> m.throw(ValueError('haha'))
27: 'dealing with the exception'
>>> m.close()
ok let's clean
>>> m.next
28: <method-wrapper 'next' of generator object at 0x01E20D50>
>>> m.next()



[size=large][color=red][b]6.groupby[/b][/color][/size]

from itertools import groupby
>>> def compress(data):
... return ((len(list(group)), name)
... for name, group in groupby(data))
...
>>> def decompress(data):
... return (car * size for size, car in data)
...
>>> list(compress('get uuuuuuuuuuuuuuuuuup'))
[(1, 'g'), (1, 'e'), (1, 't'), (1, ' '),
(18, 'u'), (1, 'p')]
>>> compressed = compress('get uuuuuuuuuuuuuuuuuup')
>>> ''.join(decompress(compressed))
'get uuuuuuuuuuuuuuuuuup'
【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)》的技术资源,聚焦于电力系统中低碳经济调度问题,结合N-1安全准则与分布鲁棒机会约束(DRCC)方法,提升调度模型在不确定性环境下的鲁棒性和可行性。该资源提供了完整的Matlab代码实现,涵盖建模、优化求解及仿真分析全过程,适用于复杂电力系统调度场景的科研复现与算法验证。文中还列举了大量相关领域的研究主题与代码资源,涉及智能优化算法、机器学习、电力系统管理、路径规划等多个方向,展示了广泛的科研应用支持能力。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事能源调度、智能电网相关工作的工程师。; 使用场景及目标:①复现高水平期刊(如EI/SCI)关于低碳经济调度的研究成果;②深入理解N-1安全约束与分布鲁棒优化在电力调度中的建模方法;③开展含新能源接入的电力系统不确定性优化研究;④为科研项目、论文撰写或工程应用提供可运行的算法原型和技术支撑。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码与案例数据,按照目录顺序逐步学习,并重点理解DRCC建模思想与Matlab/YALMIP/CPLEX等工具的集成使用方式,同时可参考文中列出的同类研究方向拓展研究思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值