python列表list(13)

发布时间:2020-04-09 11:28:10 作者:qq5d6f345f0205e
来源:网络 阅读:414

列表是python开发过程中最常用的数据类型之一,列表俗称:list ,特点如下:

1.列表由一个或者多个数据构成,数据的类型可以不相同也可以相同;

2.列表中的数据需要写在[]中括号内部,数据与数据之间用逗号隔开;

3.列表是一个有序的集合,下标索引默认重 0 开始,和字符串类似;

python列表list(13)

具体代码示例如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

# !usr/bin/env python

# -*- coding:utf-8 _*-

"""

@Author:何以解忧

@Blog(个人博客地址): shuopython.com

@WeChat Official Account(微信公众号):猿说python

@Github:www.github.com

 

@File:python_list.py

@Time:2019/9/25 20:45

 

@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

 

 

list1 = list() #定义一个空列表

print("list1 :",list1)

list2 = [1,2,3,4]           #定义一个整数类型的列表赋值给list2

print("list2 : %s" % list2)

list3 = ["a","b","c"]       #定义一个字符串类型的列表赋值给list3

print("list3 : {}" .format(list3))

list4 = [0,"hello",True]    #定义一个不同数据类型的列表赋值给list4

print("list4 : {0}" .format(list4))

输出结果:

1

2

3

4

list1 : []

list2 : [1, 2, 3, 4]

list3 : ['a', 'b', 'c']

list4 : [0, 'hello', True]

 

 

python列表list(13)

在python开发过程,list列表最常用的就是增删改查,下面跟上代码一一讲解:

一.列表List增加数据

一般可以使用append()函数来为列表list添加数据,默认将数据追加在末尾。示例代码如下:

1

2

3

4

5

list1 = list() #定义一个空列表

print("list1 : ",list1)

list1.append("hello") # 在列表list的末尾添加字符串 'hello'

list1.append(True)    # 在列表list的末尾添加布尔值 True

print("list1 : ",list1)

输出结果:

1

2

list1 :  []

list1 :  ['hello', True]

 

二.列表List删除数据

列表中的数据从左到右,索引值默认重0 开始以此递增,和字符串的索引值类似,删除使用 del 关键字,直接列表List时根据数据对应的索引值直接删除即可,代码如下:

1

2

3

4

5

6

7

8

list2 = [1,2,3,4,5,6,7,False,"python"]

print("删除数据之前:{}".format(list2))

del list2[0]  # 删除列表中的(索引值等于0)第一个数据,此时list2 中数据为[2,3,4,5,6,7,False,"python"]

print("第一次数据之后:{}".format(list2))

del list2[0]  # 基于上一次的结果,删除(索引值等于0)第一个数据,此时list2 中数据为[3,4,5,6,7,False,"python"]

print("第二次数据之后:{}".format(list2))

del list2[3]  # 基于上一次的结果,删除(索引值等于3)第四个数据,此时list2 中数据为[3,4,5,7,False,"python"]

print("第三次数据之后:{}".format(list2))

输出结果:

1

2

3

4

删除数据之前:[1, 2, 3, 4, 5, 6, 7, False, 'python']

第一次数据之后:[2, 3, 4, 5, 6, 7, False, 'python']

第二次数据之后:[3, 4, 5, 6, 7, False, 'python']

第三次数据之后:[3, 4, 5, 7, False, 'python']

 

三.列表List修改数据

直接根据索引值找到列表中对应的数据,然后赋值即可。

1

2

3

4

5

6

list2 = [1,2,3,4,5,6,7,False,"python"]

print("修改数据之前:{}".format(list2))

list2[2] = False   # 修改列表索引值为2的数据(即列表中的第三个数据),直接赋值为bool变量 False

print("第一次修改数据之后:{}".format(list2))

list2[0] = "python"  # 修改列表索引值为0的数据(即列表中的第第一个数据),直接赋值为bool变量 False

print("第二次修改数据之后:{}".format(list2))

输出结果:

1

2

3

修改数据之前:[1, 2, 3, 4, 5, 6, 7, False, 'python']

第一次修改数据之后:[1, 2, False, 4, 5, 6, 7, False, 'python']

第二次修改数据之后:['python', 2, False, 4, 5, 6, 7, False, 'python']

 

四.列表List查找数据

直接根据索引值找到列表中对应的数据即可。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

list2 = [1,2,3,4,5,6,7,False,"python"]

print(list2[0])                 # 输出列表中索引值为0的数据,即第一个元素

print(list2[5])                 # 输出列表中索引值为5的数据,即第六个元素

print(len(list2))               # 获取列表中数据个数

 

# 获取列表的最后一个元素,注意要 len(list2) - 1,因为最后一个元素的索引值为8

print("list2中最后一个数据是:",list2[len(list2)-1])

 

print("***"*20) #小窍门:直接输出60个*

# 遍历列表

print("遍历列表方式一:")

for i in  list2:

    print(i)

    

    

print("***"*20) #小窍门:直接输出60个*

print("遍历列表方式二:")

for i in range(len(list2)):

    # 内置函数 type()获取数据类型

    print("list2列表中索引值{}对应的数据是{},数据类型是:{}".format(i,list2[i],type(list2[i])))

输出结果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

1

6

9

list2中最后一个数据是: python

************************************************************

遍历列表方式一:

1

2

3

4

5

6

7

False

python

************************************************************

遍历列表方式二:

list2列表中索引值0对应的数据是1,数据类型是:<class 'int'>

list2列表中索引值1对应的数据是2,数据类型是:<class 'int'>

list2列表中索引值2对应的数据是3,数据类型是:<class 'int'>

list2列表中索引值3对应的数据是4,数据类型是:<class 'int'>

list2列表中索引值4对应的数据是5,数据类型是:<class 'int'>

list2列表中索引值5对应的数据是6,数据类型是:<class 'int'>

list2列表中索引值6对应的数据是7,数据类型是:<class 'int'>

list2列表中索引值7对应的数据是False,数据类型是:<class 'bool'>

list2列表中索引值8对应的数据是python,数据类型是:<class 'str'>

注意上面代码中两种循环方式的区别,第一种循环是直接根据列表list中的数据通过偏移依次遍历,第二种是通过列表list的索引值遍历循环,类似查找操作。顺便回忆一下内置函数type()的使用。

五.列表List截取

列表List截取和字符串的操作类似,直接根据List的下标索引值操作即可,演示代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

list1 = ["猿",True,"python",5.5,"hello",20,"list","study"]

print("list1:",list1)

 

# 截取在列表中索引值为2-4的数据,注意截取并不包括4

list2 = list1[2:4]

print("list2:",list2)

 

# 截取在列表中索引值为1-5的数据,注意截取并不包括5

list3 = list1[1:5]

print("list3:",list3)

 

# 截取在列表中索引值为0-4的数据,冒号前面不设置参数,默认重0开始,注意截取并不包括4

list4 = list1[:4]

print("list4:",list4)

 

# 截取在列表中索引值为2-末尾的数据,冒号后面不设置参数,默认截取到最后一位数据,注意截取包括最后一位

list5 = list1[2:]

print("list5:",list5)

输出结果:

1

2

3

4

5

list1: ['猿', True, 'python', 5.5, 'hello', 20, 'list', 'study']

list2: ['python', 5.5]

list3: [True, 'python', 5.5, 'hello']

list4: ['猿', True, 'python', 5.5]

list5: ['python', 5.5, 'hello', 20, 'list', 'study']

 

六.列表List排序

可以通过使用sort()函数或者reverse()函数对列表list排序,演示代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# 对数字排序

list1 = [10,2,30,4,5,6,7] #定义一个空列表

print("排序之前:",list1)

list1.sort()  # 默认重小到大依次排序

print("排序之后:%s" % list1)

list1.reverse() # 默认重大到小依次排序

print("排序之后:{}".format(list1))

 

print("***"*20) # 小窍门:直接打印60个*

#对字符串排序

list2 = ["f","e","c","a"]

print("排序之前:",list2)

list2.sort()  # 默认重小到大依次排序

print("排序之后:%s" % list2)

list2.reverse() # 默认重大到小依次排序

print("排序之后:{}".format(list2))

输出结果:

1

2

3

4

5

6

7

排序之前: [10, 2, 30, 4, 5, 6, 7]

排序之后:[2, 4, 5, 6, 7, 10, 30]

排序之后:[30, 10, 7, 6, 5, 4, 2]

************************************************************

排序之前: ['f', 'e', 'c', 'a']

排序之后:['a', 'c', 'e', 'f']

排序之后:['f', 'e', 'c', 'a']

 

七.列表List与字符串str的相互转换

1.str 字符串转列表 list

使用list(str),强制将str字符串转为list列表,演示代码如下:

1

2

3

4

str1 = "hello world"

list1 = list(str1)  # 强制将str1 字符串转为列表 list

print("str1:{},数据类型:{}".format(str1,type(str1)))

print("list1:{},数据类型:{}".format(list1,type(list1)))

输出结果:

1

2

str1hello world,数据类型:<class 'str'>

list1['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],数据类型:<class 'list'>

 

2.list 列表转字符串 str

使用join()函数将列表直接转为字符串,演示代码如下:

1

2

3

4

list2 = ["猿说python",'-',"python教程"]

str2 = "".join(list2)

print("list2:{},数据类型:{}".format(list2,type(list2)))

print("str2:{},数据类型:{}".format(str2,type(str2)))

输出结果:

1

2

list2['猿说python', '-', 'python教程'],数据类型:<class 'list'>

str2:猿说python-python教程,数据类型:<class 'str'>

 

 

八.重点总结

1.对于列表的增删改查是python开发中经常使用的内容,需要全部掌握.

2.注意列表List与字符串str的写法区别:

1

2

a = "hello world"    # 字符串

b = ["hello world"]  # 列表,列表中只有一个字符串数据

 

 

猜你喜欢:

1.Pycharm配置开发模板

2.python for循环

3.python 字符串

 

转载请注明:猿说Python » python列表

 



推荐阅读:
  1. Python的列表list
  2. Python列表list操作的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python列表 python list python列表list

上一篇:使用js判断打开项目的浏览器类别的代码

下一篇:NetScaler/MAS/XAXD自动文档生成工具

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》