当前位置: 首页 > news >正文

Python 基础——列表(list)

一.创建列表

以逗号分隔的不同数据项使用方括号括起来,即可创建列表

**普通列表

>>> list1 = ['what', 'can', 'I', 'say']
>>> list2 = ["hong", "yun", "dang", "tou", "666"]
>>> number = [1, 2, 3, 4, 5]

 **混合列表

>>> mix = [1, 'woc', 3.234, [1, 3, 'kao']]

**空列表

>>> empty = []

二.向列表添加元素

1.使用函数:append()

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.append('oh man')
>>> list1
['what', 'can', 'I', 'say', 'oh man']

 若再次输入俩个元素,则出现以下错误

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.append('oh', 'man')
Traceback (most recent call last):File "<pyshell#1>", line 1, in <module>list1.append('oh', 'man')
TypeError: list.append() takes exactly one argument (2 given)

2.使用函数:extend()

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.extend('oh', 'man')
Traceback (most recent call last):File "<pyshell#1>", line 1, in <module>list1.extend('oh', 'man')
TypeError: list.extend() takes exactly one argument (2 given)

仍然会出现错误,进行以下修改

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.extend(['oh', 'man'])
>>> list1
['what', 'can', 'I', 'say', 'oh', 'man']

(以上都是将元素追加到列表的末尾)

3.使用函数:insert()

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.insert(0, 'oh man')
>>> list1
['oh man', 'what', 'can', 'I', 'say']
#特殊
>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.insert(0, ['oh', 'man'])
>>> list1
[['oh', 'man'], 'what', 'can', 'I', 'say']

三.列表中的元素操作

1.获取元素

通过元素的索引值(index)从列表获取单个元素(列表索引值是从0开始的)

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1[0]
'what'

2.交换列表中的元素

>>> list1 = ['what', 'can', 'I', 'say']
>>> temp = list1[0]
>>> list1[0] = list1[1]
>>> list1[1] = temp
>>> list1
['can', 'what', 'I', 'say']

3.从列表中删除元素

函数remove()

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.remove('what')
>>> list1
['can', 'I', 'say']

del语句

#1.可单独删除一个元素
>>> list1 = ['what', 'can', 'I', 'say']
>>> del list1[0]
>>> list1
['can', 'I', 'say']#2.可删除一个列表
>>> list1 = ['what', 'can', 'I', 'say']
>>> del list1
>>> list1
Traceback (most recent call last):File "<pyshell#9>", line 1, in <module>list1
NameError: name 'list1' is not defined. Did you mean: 'list'?

函数pop()

#1.无索引值
>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.pop()
'say' #类似于栈,后进先出,返回删除的值
>>> list1
['what', 'can', 'I']#2.有索引值
>>> list1 = ['what', 'can', 'I', 'say']
>>> name = list1.pop(2)
>>> name
'I'
>>> list1
['what', 'can', 'say']

4.列表分片(切片)slice

一次性获取多个元素

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1[1:2]
['can']
>>> list1[1:3]
['can', 'I'] #左闭右开
>>> list1
['what', 'can', 'I', 'say'] #原列表无任何变化

四.列表的一些操作符

比较操作符

>>> list1 = [123]
>>> list2 = [234]
>>> list1 < list2
True
>>> list1 = [123, 456]
>>> lidt2 = [234, 345]
>>> list1 > lidt2
False

逻辑操作符

>>> list3 = [678]
>>> (list1 < lidt2) and (lidt2 < list3)
True
>>> (list1 < lidt2) and (lidt2 == list3)
False>>> list4 = list1 + list3 + lidt2
>>> list4
[123, 456, 678, 234, 345]

重复操作符

>>> list1 = ['what', 'can']
>>> list1 * 5
['what', 'can', 'what', 'can', 'what', 'can', 'what', 'can', 'what', 'can']
>>> list1
['what', 'can']
>>> list1 *= 3
>>> list1
['what', 'can', 'what', 'can', 'what', 'can']

成员关系操作符

>>> list1 = ['what', 'can', 'I', 'say']
>>> 'what'in list1
True
>>> 'oh man' not in list1
True
#特殊
>>> list1 = ['what', 'can', 'I', 'say', ['oh', 'man']]
>>> 'oh' in list1
False
# 只能访问一层>>> list1 = ['what', 'can', 'I', 'say', ['oh', 'man']]
>>> 'oh' in list1[4]
True>>> list1 = ['what', 'can', 'I', 'say', ['oh', 'man']]
>>> list1[4][1]
'man'

五.列表类型的内置函数

由此可得到列表的内置函数

>>> dir(list)
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

 count函数:元素出现的次数

>>> list1 = ['what', 'can', 'what', 'can', 'what', 'can', 'what', 'can', 'what', 'can']
>>> list1.count('what')
5

index函数:返回参数在列表中的位置

list1 = ['what', 'can']
list1.index('what')
0

reverse函数:反转列表

>>> list1 = ['what', 'can', 'I', 'say']
>>> list1.reverse()
>>> list1
['say', 'I', 'can', 'what']

sort函数:排序

  sort(reverse = True)

>>> list1 = [1, 3, 2, 8, 5, 6]
>>> list1.sort()
>>> list1
[1, 2, 3, 5, 6, 8]>>> list1.sort(reverse = True)
>>> list1
[8, 6, 5, 3, 2, 1]

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Xcode如何创建多个工程
  • 提高Java程序效率:ImmutableList、Stream API 和 JSON序列化实战指南
  • 【保姆级】Python项目部署到Linux生产环境(uwsgi+python+flask+nginx服务器)
  • python编程技巧——list计算
  • 继承与多态 Java
  • macOS 环境Qt Creator 快捷键
  • SimMIM:一个类BERT的计算机视觉的预训练框架
  • vue学习笔记(十)——Vuex(状态管理,组件间共享数据)
  • 极狐GitLab 如何管理 PostgreSQL 扩展?
  • 系统架构设计师教程(清华第二版) 第3章 信息系统基础知识-3.2 业务处理系统-解读
  • Android构建任务assemble、bundle、compile、package、install
  • python如何创建SQLite 数据库连接,如何将数据库存储在内存中?
  • git clone加速
  • huawei USG6001v1学习---防火墙相关知识(2)
  • Java基础编程500题——String
  • 「面试题」如何实现一个圣杯布局?
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • MySQL用户中的%到底包不包括localhost?
  • nfs客户端进程变D,延伸linux的lock
  • Octave 入门
  • Redux 中间件分析
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 学习HTTP相关知识笔记
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • 主流的CSS水平和垂直居中技术大全
  • Java总结 - String - 这篇请使劲喷我
  • Python 之网络式编程
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • ​如何在iOS手机上查看应用日志
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (BFS)hdoj2377-Bus Pass
  • (javaweb)Http协议
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (创新)基于VMD-CNN-BiLSTM的电力负荷预测—代码+数据
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (四)activit5.23.0修复跟踪高亮显示BUG
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • .Net 6.0--通用帮助类--FileHelper
  • .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
  • .NET 通过系统影子账户实现权限维持
  • @antv/x6 利用interacting方法来设置禁止结点移动的方法实现。
  • [ solr入门 ] - 利用solrJ进行检索
  • [ vulhub漏洞复现篇 ] ECShop 2.x / 3.x SQL注入/远程执行代码漏洞 xianzhi-2017-02-82239600
  • [<死锁专题>]
  • [Angular] 笔记 16:模板驱动表单 - 选择框与选项
  • [AR Foundation] 人脸检测的流程
  • [c++] 自写 MyString 类