python for loop extending from the application to derive the formula

python from traditional extension application for loop to derive the formula

In this paper, writing gradual, step by step progression.

Traditional for loop:

#获取1到1000000的偶数
#采用传统写法(俗称普通解析)
for i in range(1,10**6+1):
	if(i%2==0):
		print(i)
#程序运行结果:
#2
#4
#.
#.
#.
#1000000

Summary (of the code evaluation intuitively): to fulfill the request, but the code is simple enough.
Code improved, the list is derived using the formula:

#获取1到1000000的偶数
#采用列表推导式(俗称列表解析)
print([i for i in range(1,10**6+1) if i%2==0])
print(type([i for i in range(1,10**6+1) if i%2==0])
#程序运行结果:
#[2,4,6,8...1000000]
#<class 'list'>
#

Let us evaluate the code from the code speed.

Traditional for loop speed:

#采用clock()函数进行测量代码运行时间
#用以浮点数计算的秒数返回当前的CPU时间,用来衡量不同程序的耗时,比time.time()更精确
import time
cpu_start=time.clock()
for i in range(1,10**6+1):
	if(i%2==0):
		print(i)
cpu_end=time.clock()
print("程序运行时间:",cpu_end-cpu_start)
#程序运行结果:
#2
#4
#.
#.
#.
#1000000
#程序运行时间: 2.1866424
#注意:编译器在3.3-3.8之间的会报一个警告:DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
#因为python3.3以后不被推荐使用,该方法依赖操作系统,建议使用per_counter(返回系统运行时间)或process_time(返回进程运行时间)代替

Derived using a list of the running speed of the formula:

#采用clock()函数进行测量代码运行时间
#用以浮点数计算的秒数返回当前的CPU时间,用来衡量不同程序的耗时,比time.time()更精确
import time
cpu_start=time.clock()
print([i for i in range(1,10**6+1) if i%2==0])
cpu_end=time.clock()
print("程序运行时间:",cpu_end-cpu_start)
#程序运行结果:
#[2,4,6,8...1000000]
#程序运行时间: 0.005884400000000012
#注意:编译器在3.3-3.8之间的会报一个警告:DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
#因为python3.3以后不被推荐使用,该方法依赖操作系统,建议使用per_counter(返回系统运行时间)或process_time(返回进程运行时间)代替

Summary (evaluated from code running speed): Obviously using a list comprehension of the code, the speed is superior.
Conclusion:
1. compiler optimization, will not affect the efficiency of shorthand, but because of optimization to improve the efficiency.
2. To reduce the workload of programmers and reduce errors.
3. That simplifies the code, but also enhance the readability of the code.

Then there is the existence of a set of analytical formula list comprehensions, dictionary comprehensions, tuples derivation, let's check it out now! There is no longer explore their speed up!
Collection derivations:

#集合推导式
#获取1到1000000的偶数
x={i for i in range(1,10**6+1) if i%2==0}
type(x)
#输出结果:
#{2,4,6,8...1000000}
#<class 'set'>

Dictionary derivations:

#获取值是偶数的所有键值对形成的字典
#采用传统方法
dict={'名字1': 1, '名字二': 2, '名字三': 3, '名字四': 4}
dict_x={}
for i,j in dict.items():
    if(j%2==0):
        dict_x[i]=j
print(dict_x)
print(type(dict_x))
#{'名字二': 2, '名字四': 4}
#<class 'dict'>
#获取值是偶数的所有键值对形成的字典
#字典推导式
#方法一
#采用zip函数与for循环将两个列表的值逐步遍历作为字典的键与值
x = {i : j for i, j in zip(["名字1", "名字二", "名字三", "名字四"], [1,2,3,4]) if j%2==0}
print(x)
print(type(x))
#运行结果:
#{'名字二': 2, '名字四': 4}
#<class 'dict'>

#方法二
#获取值是偶数的所有键值对形成的字典
#采用items()函数进行迭代遍历
dict={'名字1': 1, '名字二': 2, '名字三': 3, '名字四': 4}
x = {i : j for i, j in dict.items() if j%2==0}
print(x)
print(type(x))
#运行结果:
#{'名字二': 2, '名字四': 4}
#<class 'dict'>

Tuple derivations:

#获取1到1000000的偶数
x=(i for i in range(1,10**6+1) if i%2==0)
print(x)
type(x)
#输出结果:
#<generator object <genexpr> at 0x00000241FFAB2750>
#<class 'generator'>

what! Front return array or collection or dictionary, how the returned generator (genexpr), since tuples are immutable. We look to access the inside of the elements!

#获取值是偶数的所有键值对形成的字典
x=(i for i in range(1,10**6+1) if i%2==0)
for i in x:
	print(i)
#输出结果:
#2
#4
#.
#.
#.
#1000000

Summary: derivation Although there are many advantages, but the only downside is that local variables are not high readability.

To wave, push it!
Group number: 781 121 386
group name: Life is short, I learned programming
welcome to join us, along with the exchange of technology !!!

Published 38 original articles · won praise 128 · views 10000 +

Guess you like

Origin blog.csdn.net/lujiangyang123/article/details/103253287