Python 推导

在本文中,我们将看到如何推导 Python 的数据结构,如列表、字典、集合和生成器。

推导提供了一种用 Python 编写程序的精确方式。它在不影响代码可读性的情况下减少了代码大小。

所以,在这里我们将讨论以下推导-

  1. 列表推导
  2. 字典推导
  3. 集合推导
  4. 生成器推导

列表推导

我们知道列表的元素被括在方括号中,它可以保存多种数据类型的值。

在下面给出的程序中,我们将从列表中取出偶数。

让我们看看下面的列表示例。

示例-


list1= [20,25,24,30,35,40,44]
list2=[]
for i in list1:
    if i%2==0:
        list2.append(i)
print("The elements of list2 are :" ,list2)

输出-

The elements of list2 are : [20, 24, 30, 40, 44]

这里我们已经指定了 list1 的元素,然后使用 for循环来获取每个元素,并使用模数运算符检查它是否能被 2 整除。

在下面给出的程序中,我们使用列表推导进行了同样的操作。

示例-2-使用列表推导


list1=[20,25,24,30,35,40,44]
list2=[i for i in list1 if i%2==0]
print("The elements obtained using list comprehension are :" ,list2)

输出-

The elements obtained using list comprehension are : [20, 24, 30, 40, 44]

在这里,我们可以观察到,我们在清单 2 中提供了推导,我们在一行中使用了循环和决策。

下一个程序是基于获取列表 1 中所有元素的立方体。

示例- 3


list1=[2,3,4,5,6,7,8,9,10]
list2=[]
for i in list1:
    list2.append(i**3)
print("The cube of the elements present in list1 is: ",list2)

输出-

The cube of the elements present in list1 is:  [8, 27, 64, 125, 216, 343, 512, 729, 1000]

我们使用 append()方法,将每个元素的立方体存储在列表 2 中,然后显示它。

我们可以用列表推导来做同样的事情-

示例-4-使用列表推导


list1=[2,3,4,5,6,7,8,9,10]
list2=[i**3 for i in list1 ]
print("The cube of the elements obtained by list comprehension is: ",list2)

输出-

The cube of the elements obtained by list comprehension is:  [8, 27, 64, 125, 216, 343, 512, 729, 1000]

字典推导

我们都知道字典使用键值对,让我们来看看显示这些键值对的程序。

示例-


fruits=['Apple','Bananas','Custard Apple','Pineapple','Blueberries']
color=['Red','Yellow','Green','Brown','Violet']
result_dict={}
for key,value in zip(fruits,color):
    result_dict[key]=value
print("The resultant dictionary would be: ",result_dict)

输出-

The resultant dictionary would be:  {'Apple': 'Red', 'Bananas': 'Yellow', 'Custard Apple': 'Green', 'Pineapple': 'Brown', 'Blueberries': 'Violet'}

在下一个程序中,同样的事情用推导来实现

示例-2-使用字典推导


fruits=['Apple','Bananas','Custard Apple','Pineapple','Blueberries']
color=['Red','Yellow','Green','Brown','Violet']
result_dict={key:value for (key,value) in zip(fruits,color)}
print("The resultant dictionary using comprehension would be: ",result_dict)

输出-

The resultant dictionary using comprehension would be:  {'Apple': 'Red', 'Bananas': 'Yellow', 'Custard Apple': 'Green', 'Pineapple': 'Brown', 'Blueberries': 'Violet'}

在这里,我们可以观察到我们在 result_dict 中提供了推导,其中我们给出了用于显示来自列表水果和颜色的键值对的表达式。

集合推导

Set 用于显示给定集合中的唯一元素。让我们用集合获得列表所有元素的平方。

示例- 1


list1=[2,3,4,5,6,7,8,9,10]
result_set=set()
for i in list1:
    result_set.add(i**2)
print("The square of the numbers present in list1 is: ",result_set)

输出-

The square of the numbers present in list1 is:  {64, 4, 36, 100, 9, 16, 49, 81, 25}

在下面给出的程序中,我们用推导做了同样的事情。

示例- 2-使用集合推导


list1=[2,3,4,5,6,7,8,9,10]
result_set={i**2 for i in list1}
print("The square of the numbers obtained through set comprehension: ",result_set)

输出-

The square of the numbers obtained through set comprehension:  {64, 4, 36, 100, 9, 16, 49, 81, 25}

我们从列表 1 中提取了每个元素,并在 result_set 中提供了计算这些元素平方的表达式。

生成器推导

生成器的功能非常相似。它使用 yield 关键字生成一个值。让我们看看推导在这里是如何运用的。

示例-


list1=[12,16,17,20,21,24,28,30,31]
result_gen=(i for i in list1 if i%2==0)
for i in result_gen:
    print("The element which is even in list1 is: ",i)

输出-

The element which is even in list1 is:  12
The element which is even in list1 is:  16
The element which is even in list1 is:  20
The element which is even in list1 is:  24
The element which is even in list1 is:  28
The element which is even in list1 is:  30

在执行程序时,它显示列表 1 中的偶数元素。


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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
QMZFS的头像QMZFS
上一篇 2025-01-09 12:14
下一篇 2025-01-09 12:14

相关推荐

  • Python周杰伦代码用法介绍

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

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

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

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

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

    编程 2025-04-29
  • Python中引入上一级目录中函数

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

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

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

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

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

    编程 2025-04-29
  • Python编程二级证书考试相关现已可以上网购买

    计算机二级Python考试是一项重要的国家级认证考试,也是Python编程的入门考试。与其他考试一样,Python编程二级证书的考生需要进入正式考试,而为了备考,这篇文章将详细介绍…

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

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

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

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

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

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

    编程 2025-04-29

发表回复

登录后才能评论