python入门基础知识五(for循环、公共操作与推导式)

for循环

break终止循环

a = 'abcd'
for i in a:
...     if i == 'c':
...         print("'c' stands for 'cease'")
...         break
...     print(i)
...

a
b
'c' stands for 'cease'

continue退出本次循环,继续执行下一个循环

for i in a:
...     if i == 'c':
...         print("'c' stands for 'cease'")
...         continue
...     print(i)
...

a
b
'c' stands for 'cease'
d

for…else

for i in array:

codes for loop

else:

if exit loop normally then run these codes…

for i in a:
...     if i == 'c':
...         print("'c' stands for 'cease'")
...         break
...     print(i)
... else:
...     print('This loop has ended normally.')
...

a
b
'c' stands for 'cease'    # 循环没有正常结束,没有返回预先写好的提示

for i in a:
...     if i == 'c':
...         print("'c' stands for 'cease'")
...         continue
...     print(i)
... else:
...     print('This loop has ended normally.')
...

a
b
'c' stands for 'cease'
d
This loop has ended normally.    # 循环正常结束,返回提示

公共操作

运算符

python入门基础知识五(for循环、公共操作与推导式)

公共方法:支持字符串、列表、元组、集合、字典

python入门基础知识五(for循环、公共操作与推导式)

enumerate

作用是将要给可遍历的数据对象(如,列表、元组或字符串)组合为一个索引序列,同时列出数据及其下标,一般与for循环联用。

enumerate(可遍历对象, start = 0) # 下标起始默认值为0,可不写

a = ['sun','moon','star']
for i in enumerate(a):
...     print(i)
...

(0, 'sun')
(1, 'moon')
(2, 'star')
for i in enumerate(a,start = 1):
...     print(i)
...

(1, 'sun')
(2, 'moon')
(3, 'star')
for i,j in enumerate(a):
...     print(f'The index is {i}, corresponding data is {j}')
...

The index is 0, corresponding data is sun
The index is 1, corresponding data is moon
The index is 2, corresponding data is star

推导式

主要作用是简化代码

列表的推导式

创建一个由0-10组成的列表,如果不用推导式,就必须令 a = [1,2,3,4…10]。如果创建一个从0-100的列表呢?

range(起,终,步长) # 步长如果是1可以省略不写,注意,终和下标是一样的,如果要创建0-10的数组,必须要把终的位置写在11!

a = []
for i in range(0,11):
...     a.append(i)
...     print(a)    # print的对齐位置很重要
...

[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a = []
for i in range(0,11):
...     a.append(i)
... print(a)    # print的对齐位置很重要
...

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

带条件语句的列表推导式

创建一个0-10以内偶数组成的列表

a = [i for i in range(0,11,2)]
print(a)
[0, 2, 4, 6, 8, 10]

a = [i for i in range(0,11) if i%2 == 0]    # 除以2,余数为0的i
print(a)[0, 2, 4, 6, 8, 10]

字典推导式

用于将两个列表合并为字典,或提取字典中的目标数据

list1 = ['beef','mutton','pork']
list2 = [55,45,22]
a = {list1[i]:list2[i] for i in range(len(list2))}
print(a)
{'beef': 55, 'mutton': 45, 'pork': 22}

list1.append('fish')
list1
['beef', 'mutton', 'pork', 'fish']

a = {list1[i]:list2[i] for i in range(len(list2))}
print(a)
{'beef': 55, 'mutton': 45, 'pork': 22}

a = {list1[i]:list2[i] for i in range(len(list1))}  # 注意,len要用最短的那个列表,否则会报错
Traceback (most recent call last):
  File "", line 1, in
  File "", line 1, in
IndexError: list index out of range
b = {i:j for i,j in a.items() if j > 40}  # i代表的是key,j代表的是value
print(b)
{'beef': 55, 'mutton': 45}

集合推导式

不常用,主要是数字的运算

c = {2,2,3}
d = {i ** 3 for i in c}
print(d)
{8, 27}    # 集合自动去重,进行了3次方的运算

Original: https://www.cnblogs.com/randyszone/p/16520243.html
Author: randy198
Title: python入门基础知识五(for循环、公共操作与推导式)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/583076/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Tomcat性能优化方案

    你使用过tomcat的话,简单的说就是”内存溢出”. 通常情况下,这种问题出现在实际的生产环境中.产生这种问题的原因是tomcat使用较少的内存给进程,通过…

    Linux 2023年6月14日
    0117
  • 线程池如何保证核心线程一直存活

    转载请注明出处: 查看 ThreadPoolExecutor 类中的 getTask 方法,这个方法可以保持核心线程在没有任务的时候也可以一直处于存活状态 核心在于 workQue…

    Linux 2023年6月14日
    0164
  • 【转】我是一个CPU:这个世界慢!死!了!

    简介 我经常听到人们说磁盘慢,网络很慢,这是从人类感知的角度来表达的。比如,把一个文件拷贝到硬盘上需要几分钟到几十分钟,足够我吃一顿饭;而从网上下载一部电影,有时需要几个小时,我可…

    Linux 2023年5月27日
    0108
  • 内核同步问题

    linux内核同步问题 Linux内核设计与实现 十、内核同步方法 [手把手教Linux驱动5-自旋锁、信号量、互斥体概述](https://www.cnblogs.com/yik…

    Linux 2023年6月13日
    089
  • Harbor部署

    harbor 无论是使用Docker-distribution去自建仓库,还是通过官方镜像跑容器的方式去自建仓库,通过前面的演示我们可以发现其是非常的简陋的,还不如直接使用官方的D…

    Linux 2023年6月7日
    0112
  • N68第二周作业

    完成作业:完成一个shell脚本,脚本的作用。1. 运行脚本可以显示出本机的ip地址2. 如果ip地址中有3这个数字,那么就打印出当前的系统时间3. 如果ip地址中不含3这个数字,…

    Linux 2023年6月7日
    0164
  • macbook air 2019 安装win10单系统

    目前不考虑写的太详细了,如果有同学遇到问题了我再完善,主要是把遇到的坑讲下第一步,准备2个U盘(如果不嫌麻烦一个也可以)1.用大白菜或者老毛桃将其中一个做成启动盘2.在window…

    Linux 2023年6月14日
    0138
  • Hadoop 调优

    Hadoop 调优 HDFS 调优 hdfs-site.xml 1. hadoop 文件块大小,通常为 128MB 或 256MB dfs.block.size 134217728…

    Linux 2023年6月8日
    0107
  • 使用github action发布hexo博客到云服务器

    > node -v v16.15.0 > npm -v 8.5.5 安装Hexo CLI 在你的&am…

    Linux 2023年6月7日
    0124
  • Golang环境安装

    一、下载地址 Golang: Downloads – The Go Programming Language GoLand编辑器: Download GoLand: A…

    Linux 2023年6月13日
    0112
  • Django基础学习笔记

    创建一个django项目:命令: django-admin startproject 项目名 进入到项目并创建一个应用:命令: python manage.py startapp …

    Linux 2023年6月6日
    0101
  • RabbitMQ知识简单理解

    官网链接:http://next.rabbitmq.com/getstarted.html 官网给出了每种工作模式的实例代码,可以参考其中的实现 一、RabbitMQ整体架构图 二…

    Linux 2023年6月14日
    0120
  • JuiceFS 在 Elasticsearch/ClickHouse 温冷数据存储中的实践

    企业数据越存越多,存储容量与查询性能、以及存储成本之间的矛盾对于技术团队来说是个普遍难题。这个难题在 Elasticsearch 与 ClickHouse 这两个场景中尤为突出,为…

    Linux 2023年6月14日
    0134
  • Linux C/C++ 获取进程号、线程号和设置线程名

    在Linux开发过程中,设计多线程开发时可以将进程和线程的 id 打印出来,方便开发调试和后期查问题使用,同时也包括设置线程名。 2.1 进程ID #include <uni…

    Linux 2023年6月7日
    0140
  • java调用python的惨痛史(无法获取环境变量)

    环境:java,was,python2.6,红帽linux,oracle,python用cx_Oracle事情是这样的,有个需求,需要对数据库进行处理,简单说就是把数据取出来,用p…

    Linux 2023年6月6日
    091
  • Question03-查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

    * SELECT stu.SID, stu.Sname, CAST(AVG(sc.score) AS DECIMAL(18,2)) avg_score FROM Student s…

    Linux 2023年6月7日
    094
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球