Python 基础——列表(list)

news/2024/8/26 13:01:32 标签: python, 学习

一.创建列表

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

**普通列表

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

 **混合列表

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

**空列表

python">>>> empty = []

二.向列表添加元素

1.使用函数:append()

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

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

python">>>> 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()

python">>>> 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)

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

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

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

3.使用函数:insert()

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

三.列表中的元素操作

1.获取元素

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

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

2.交换列表中的元素

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

3.从列表中删除元素

函数remove()

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

del语句

python">#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()

python">#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

一次性获取多个元素

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

四.列表的一些操作符

比较操作符

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

逻辑操作符

python">>>> list3 = [678]
>>> (list1 < lidt2) and (lidt2 < list3)
True
>>> (list1 < lidt2) and (lidt2 == list3)
False

>>> list4 = list1 + list3 + lidt2
>>> list4
[123, 456, 678, 234, 345]

重复操作符

python">>>> 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']

成员关系操作符

python">>>> list1 = ['what', 'can', 'I', 'say']
>>> 'what'in list1
True
>>> 'oh man' not in list1
True
python">#特殊
>>> 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'

五.列表类型的内置函数

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

python">>>> 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函数:元素出现的次数

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

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

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

reverse函数:反转列表

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

sort函数:排序

  sort(reverse = True)

python">>>> 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]


http://www.niftyadmin.cn/n/5558051.html

相关文章

初识Docker及管理Docker

Docker部署 初识DockerDocker是什么Docker的核心概念镜像容器仓库 容器优点容器在内核中支持2种重要技术&#xff1a;Docker容器与虚拟机的区别 安装Docker源码安装yum安装检查Docker Docker 镜像操作配置镜像加速器&#xff08;阿里系&#xff09;搜索镜像获取镜像查看镜像信息…

用户excel对CAN进行图形化展示

在Excel中对CAN数据进行图形化展示&#xff0c;用户可以通过以下几个步骤来实现&#xff1a; 一、数据准备 导出CAN数据&#xff1a;首先&#xff0c;需要将CAN数据从CAN分析工具或设备中导出为Excel支持的格式&#xff08;通常是.xlsx或.csv&#xff09;。大多数CAN系列工具软…

gemini-pro-vision 看图说话

一、安装 pip install -U langchain-google-vertexai 二、设置访问权限 申请服务账号json格式key 三、完整代码 import gradio as gr import json import base64 from pathlib import Path import os import time import requests from fastapi import FastAPI, UploadFile,…

C++客户端Qt开发——常用控件QWidget

四、常用控件 属性 作用 enabled 设置控件是否可使用.true 表⽰可用&#xff0c;false 表示禁用 geometry 位置和尺寸&#xff0c;包含x,y,width,height四个部分 其中坐标是以⽗元素为参考进行设置的. windowTitle 设置widget标题 windowIcon 设置widget图标 windowO…

线性代数:多个随机变量相加的方差

对于多个随机变量相加的方差&#xff0c;我们可以按照独立和不独立的情况进行讨论。 1. 独立情况下的证明 假设我们有 n n n 个独立的随机变量 X 1 , X 2 , … , X n X_1, X_2, \ldots, X_n X1​,X2​,…,Xn​。 根据方差的性质&#xff0c;如果随机变量是独立的&#xff…

eclipse 新建类class文件增加copyright版权信息

1、Window -> Preferences 2、输入code,找到code templates Java > Code Style > Code Templates 比如进行如何的设置&#xff1a; 3、新增类文件&#xff0c;会自动增加版权&#xff1a;

在 Node.js 中使用 axios 配置代理并实现图片并发下载

文章目录 一、创建 Axios 实例二、图片并发下载三、参考资料 一、创建 Axios 实例 可以创建一个 axiosConfig.ts 文件用于创建和更新相关实例&#xff1a; // server/utils/axiosConfig.ts const axios require("axios"); const { HttpsProxyAgent } require(htt…

使用milvus-sdk-go的迭代器导出数据

使用milvus-sdk-go的迭代器导出数据 迭代器是一种功能强大的工具&#xff0c;可帮助您使用主键值和布尔表达式迭代集合中的大量数据或所有数据。这可以显著改善您检索数据的方式。与传统的offset和limit参数用法不同&#xff0c;后者可能会随着时间的推移而变得效率低下&#…