【python】list 的用法

简介:
定义及初始化生成List对象
shopList = []
或者
shopList = ['apple','mango','banana','carrot']
 
从List中取出对象,类似其他开发语言中的数组用法用listname[index],代表从前开始取第index个对象(第一个从0开始记),不同的是多了一种用负数取对象listname[-index],代表从后开始取第index个对象(第一个从-1开始记)
>>> shoplist[0]
'apple'
>>> shoplist[-1]
'carrot'
其实负数 A取法可以换算为:listname[len(listname) + A]
 
从List中截取部分对象生成新List:listname[fromIndex:toIndex]代表取出索引从fromIndex到toIndex,但不包括toIndex的元素,这个也叫做List的分片
shopList[0:2]取出从0开始到第2个元素但不包括shopList[2],所以结果就相当于取出了shopList[0]和shopList[1]组合成的新List
结果为['a','b']
‘:’两边的值可以省略,前面省略代表0,后面省略代表List长度
shopList[0:2]相当于shopList[:2]
>>> shoplist[2:]
['banana', 'carrot']
>>> shoplist[1:3] 
['mango', 'banana']
 
向List添加对象有3种方法
1)向List后面添加一个对象:listname.append(obj)
例:
shopList = ['apple','mango','banana','carrot']
>>> shoplist.append('meat')
>>> shoplist
['apple', 'mango', 'banana', 'carrot', 'meat']
2)向List中间指定索引位置插入一个对象:listname.insert(index,obj)
例:
shopList = ['apple','mango','banana','carrot']
>>> shoplist.insert(3,'rice')
>>> shoplist
['apple', 'mango', 'banana', 'rice', 'carrot', 'meat']
3)向List中添加一个List中的多个对象,相当于两个List相加:listname.extend(listname2)
例:
shopList = ['apple','mango','banana','carrot']
>>> shoplist2=['todou','maizi']
>>> shoplist2.extend(shoplist)
>>> shoplist2
['todou', 'maizi', 'apple', 'mango', 'banana', 'rice', 'carrot', 'meat']

4)判断某个对象是否在List中:obj in listname
例:
shopList = ['apple','mango','banana','carrot']
>>> 'apple' in shoplist
True
 
5)在List中搜索对象:listname.index(obj)
注:如果List中存在多个相同obj,则返回的是首次出现的索引,如果List中不存在该对象会抛出异常,所以查找对象之前一定要先判断一下对象是否在List中,以免引起程序崩溃
例:
shopList = ['apple','mango','banana','carrot']
>>> shoplist.index('apple')
0
>>> shoplist.index('rice') 

7)删除List中的对象:listname.remove(obj)
注:如果List中存在多个相同obj,则删除的是首次出现的对象,如果List中不存在该对象则会抛出异常,所以在删除之前也要判断对象是否在List中
例:
>>> shoplist.remove('apple')
>>> shoplist
['mango', 'banana', 'rice', 'carrot']

 
对List进行运算
1)相加:listname1 + listname2 
注:结果为新生成一个List,并没有修改之前的两个List
例:
>>> shoplist + shoplist2
['mango', 'banana', 'rice', 'carrot', 'meat', 'todou', 'maizi', 'apple', 'mango', 'banana', 'rice', 'carrot', 'meat', 'todou', 'maizi', 'apple', 'mango', 'banana', 'rice', 'carrot', 'meat']
2)倍数:listname*n
>>> shoplist * 2
['mango', 'banana', 'rice', 'carrot', 'mango', 'banana', 'rice', 'carrot']
注意:没有减法:
>>> shoplist - shoplist2
Traceback (most recent call last):
  File "", line 1, in ?
相关文章
|
29天前
|
安全 C#
C# List基本用法
C# List基本用法
|
1月前
|
测试技术 Python
Python中的装饰器:概念、用法和应用
【4月更文挑战第6天】 装饰器是Python中的一个重要概念,它允许我们在不修改原始函数代码的情况下,增加或修改函数的行为。本文将深入探讨装饰器的概念、用法和应用,帮助读者更好地理解和使用这一强大的工具。
|
1天前
|
Python
【Python 基础】列表(list)和元组(tuple)有什么区别?
【5月更文挑战第6天】【Python 基础】列表(list)和元组(tuple)有什么区别?
|
8天前
|
Python 容器
Python中的for循环用法详解,一文搞定它
Python中的for循环用法详解,一文搞定它
|
14天前
|
缓存 Python
Python 标准库functools高阶函数用法
Python 标准库functools高阶函数用法
34 1
|
14天前
|
机器学习/深度学习 缓存 程序员
Python包管理工具 pip 及其常用命令和参数用法
Python包管理工具 pip 及其常用命令和参数用法
59 0
|
14天前
|
存储 索引 Python
多数pythoneer只知有列表list却不知道python也有array数组
多数pythoneer只知有列表list却不知道python也有array数组
19 0
|
21天前
|
索引 容器
06-python数据容器-list列表定义/list的10个常用操作/列表的遍历/使用列表取出偶数
06-python数据容器-list列表定义/list的10个常用操作/列表的遍历/使用列表取出偶数
|
25天前
|
Python
python教程:二维列表(list)初始化
python教程:二维列表(list)初始化
9 0
|
29天前
|
Python
Python中的r字符串前缀及其用法详解
Python的r字符串前缀用于创建原始字符串,不解析转义字符。在处理文件路径、正则表达式和特殊字符时特别有用。例如,`r'C:\path'`会保持反斜杠原样,而`'\n'`会被解释为换行。r字符串前缀不能用于变量或表达式,且仅影响字符串本身。了解这一特性有助于编写更清晰、准确的代码。
42 0