编程学习网 > 编程语言 > Python > python循环教程(Python的5个循环技巧)
2023
07-08

python循环教程(Python的5个循环技巧)

在这篇文章中,你将看到5种方法可以使你的python循环更习惯,运行得更快,内存效率更高。

在我看来,Python是计算机科学中最简单、最通用的语言之一。如果你正确地编写python代码,很难区分python代码和伪代码。但有时,在编写最漂亮的python代码的过程中,大多数开发人员会忘记一件事:代码的执行速度。

您可以编写可读性非常强的代码,而外行可能会将其混淆为语法技能较差的人所写的英语,但它是可以理解的。但该代码需要超过300毫秒才能运行。这可能不会造成太大的延迟,但在编程领域,这是一个严重的问题。

另一方面,可以用不同的习惯用法编写相同的代码,运行所需时间少于10毫秒。但是,即使是专业的python开发人员也很难理解它。

因此,对于开发人员来说,在这两个极端之间取得平衡是非常重要的。这些类型的代码在业界被称为惯用代码。

根据定义,惯用代码是一种用您的语言的通用方式执行通用任务的代码。换句话说,惯用代码是任何易于阅读,同时又非常高效的代码。

我在twitter上经常关注Raymond Hettinger (@raymondh),他是Python社区的核心开发人员之一,他为Python存储库贡献了大量代码,使Python语言更加习惯化。

在本博客中,我将介绍一些帮助您改进python代码的技术。开始:

◆a. range()
让我们从最明显的开始,假设你需要从2迭代到10。所以如果你是一个绝对的初学者,你可能会写这样的东西:

for i in [2,3,4,5,6,7,8,9,10]:
    print(i)
这样的方法是可行的,也是可以理解的,它不是一个可扩展的方法。如果你想循环2到200万呢。

在这种情况下,可以使用range(stop)或range(start, stop, [step,])内置函数。

range函数自动为您生成列表。例如,你可以将上面的代码改写为:

for i in range(2, 11):
    print(i)
注意我是怎么写的,从2开始到11结束,而不是10。这是因为range函数循环到stop - 1。在距离函数中除了开始和停止还有一个参数,那就是步长。步长决定范围必须跳过多少个数字。

假设您需要打印从2到10的所有偶数,在这种情况下,skip参数将为2。缺省值是1。

for i in range(2,11,2):
    print(i)

# OUTPUT: 2 4 6 8 10
现在假设您需要遍历一个列表。有了range()的知识,你可以这样做:

cloths = ['shirt', 'hat', 'socks', 'shorts']

for i in range(len(cloths))
    print(cloths[i])
但在python中,有更好的方法:

cloths = ['shirt', 'hat', 'socks', 'shorts']

for cloth in cloths
  print(cloth)
◆b. zip()
假设你有两个不同尺寸的列表,颜色和布料,你想要配对它们,直到较小的列表结束。在这种情况下,你可以写这样的代码:

colours = ['pink', 'red', 'green']
cloths = ['shirt', 'hat', 'socks', 'shorts']

n = min(len(colours), len(cloths))
for i in range(n):
    print(colours[i], cloths[i])

# OUTPUT
# pink shirt
# red hat
# green socks
这很好,但你也可以使用内置函数zip(*iterables)。这个函数最好的一点是,你可以传递任意数量的列表,而不仅仅是两个。让我重写上面的代码,然后向您解释zip是如何工作的。

colours = ['pink', 'red', 'green']
cloths = ['shirt', 'hat', 'socks', 'shorts']

for colour, cloth in zip(colours, cloths):
    print(colour, cloth)

# OUTPUT
# pink shirt
# red hat
# green socks
您可能已经猜到,zip接受任意数量的列表并返回包含每个列表中的一个元素的另一个列表。如您所见,两个版本产生相同的输出,但第二个版本看起来更干净。

◆c. reversed()
如果你想反向循环一个列表,传统的方法是这样的。

cloths = ['shirt', 'hat', 'socks', 'shorts']

for i in range(len(cloths)-1, -1, -1):
    print(cloths[i])
    
# Output
# -------
# shorts
# socks
# hat
# shirt
但是在Python中,您可以使用名为reversed()的内置函数。看看这个例子:

cloths = ['shirt', 'hat', 'socks', 'shorts']

for cloth in reversed(cloths):
    print(cloth)
    
# Output
# -------
# shorts
# socks
# hat
# shirt
第二个比第一个更干净更快。

◆d. enumerate()
您希望循环通过一个列表和索引。这在传统编程中非常直接:

cloths = ['shirt', 'hat', 'socks', 'shorts']

for i in range(len(cloths)):
    print(i, cloths[i])

# Output
# -------
# 0 shorts
# 1 socks
# 2 hat
# 3 shirt
但在python中有一种更干净、更有效的方法:

cloths = ['shirt', 'hat', 'socks', 'shorts']

for i, cloth in enumerate(cloths):
    print(i, cloth)

# OUTPUT
# -------
# 0 shorts
# 1 socks
# 2 hat
# 3 shirt
◆e. sorted()
您需要按排序顺序遍历列表,而不是运行排序算法,您可以使用sorted()内置方法。

-in method.

nums = [2,3,1,5,2]

for i in sorted(nums):
    print(i)

# OUTPUT
# ------
# 1
# 2
# 2
# 3
# 5
python排序函数使用Tim Sort,其平均复杂度为n*log(n)

对于反向排序,可以使用sorted属性(nums, reverse=True)

你想对字符串列表排序。

cloths = ['shirt', 'hat', 'socks', 'shorts']

for cloth in sorted(cloths):
    print(cloth)

# OUTPUT
# ------
# hat
# shirt
# shorts
# socks
它会根据字母排序,但如果你想根据字符串的长度排序,你可以用key attribute。例如:

cloths = ['shirt', 'hat', 'socks', 'shorts']

for cloth in sorted(cloths, key=len):
    print(cloth)

# OUTPUT
# ------
# hat
# shirt
# socks
# shorts

以上就是5种可以让你的python代码更习惯的技巧。

以上就是python循环教程(Python的5个循环技巧)的详细内容,想要了解更多Python教程欢迎持续关注编程学习网。

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取