Python 中的循环技术

Python 有特定的内置函数,因此它支持多个顺序容器中的许多循环技术。这些循环函数和方法对于竞争性编码非常有用。它可以在不同的项目中使用,在这些项目中,用户必须使用一些特定的循环技术来维护程序的整个结构。这些技术有助于节省时间和内存空间,因为用户不必为传统的循环方法声明额外的变量。

这些循环技术用在哪里?

不同类型的循环技术用于用户不必操作代码结构或整个容器顺序的地方。相反,用户必须打印单次使用实例的元素,并且容器中不会发生原地更改。这些也可以用来节省时间和内存。

使用 Python 数据结构的循环技术:

  • enumerate():enumerate()函数用于循环遍历容器,并使用特定索引中的值打印索引号。

示例:


for key, value in enumerate(['Joe', 'waited', 'for', 'the', 'train', ',', 'but', 'the', 'train', 'was', 'late', '.']):
    print(key, value)

输出:

0 Joe
1 waited
2 for
3 the
4 train
5 ,
6 but
7 the
8 train
9 was
10 late
11 .
  • zip():zip()函数用于通过按顺序打印两个相似容器的值来组合这两个容器,如 dict 与 dict 或 list 与 list。该循环仅在较小容器结束后存在。

示例:


# first, we will initialize the list
question = ['animal', 'shape', 'time']
answer = ['tiger', 'square', '11 o clock']

# the zip() function will be used for combining these two containers 
for question, answer in zip(question, answer):
    print('What is this {0}?  this is {1}.'.format(question, answer))

输出:

What is this animal?  this is tiger.
What is this shape?  this is square.
What is this time?  this is 11 o clock.
  • items():items()函数用于循环遍历字典,并按顺序打印关键字及其值。

示例:


dict = { "Joe" : "waited", "for" : "the", "train" : "but", "the" : "train", "was" : "late" }

# the use items for print the dictionary key-value pair
print ("The key value pair by using items is : ")
for a, b in dict.items():
    print(a, b)

输出:

The key value pair by using items is : 
Joe waited
for the
train but
the train
was late
  • sorted():sorted()函数用于按排序顺序打印容器。这个函数不排序容器,但是它用于按照排序的顺序打印容器。用户可以使用 set()函数和 sorted()函数来删除输出中的重复值。

例 1:


# first, initialize the list
list = [ 1 , 4, 6, 7, 1, 2, 4 ]

# using sorted() to print the list in sorted order
print ("The list in sorted order is : ")
for a in sorted(list) :
    print (a, end = " ")

print ("\r")

# now use the sorted() function and set() function for printing the list in sorted order
# use of set() to removes duplicates in output value.
print ("The list in sorted order (without duplicates) is : ")
for a in sorted(set(list)) :
    print (a, end = " ")

输出:

The list in sorted order is : 
1 1 2 4 4 6 7 
The list in sorted order (without duplicates) is : 
1 2 4 6 7

例 2:


# initializing list
list2 = ['Joe', 'waited', 'for', 'the', 'train', 'but', 'the', 'train', 'was', 'late']

# now use the sorted() function and set() function for printing the list in sorted order
for sentence in sorted(set(list2)):
    print(sentence)

输出:

Joe
but
for
late
the
train
waited
was
  • reversed():reserved()函数用于以相反的顺序打印容器的值。

例 1:


# initializing list
list = [ 1 , 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 ]

# by using the revered() function for printing the list in reversed order
print ("The list in reversed order is : ")
for a in reversed(list) :
    print (a, end = " ")

输出:

The list in reversed order is : 
21 19 17 15 13 11 9 7 5 3 1

例 2:


for b in reversed(range(1, 20, 2)):
    print (b)

输出:

19
17
15
13
11
9
7
5
3
1

结论

在本教程中,我们讨论了有助于节省内存和时间的不同类型的裁剪技术。


原创文章,作者:ZG8R0,如若转载,请注明出处:https://www.506064.com/n/128529.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
ZG8R0ZG8R0
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29

发表回复

登录后才能评论