码迷,mamicode.com
首页 > 编程语言 > 详细

15、python基础学习-列表

时间:2019-05-26 00:05:58      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:int   date   append   基础   pen   file   error   bsp   开始   

 1 #!/usr/bin/env python
 2 #__author: hlc
 3 #date: 2019/5/25
 4 
 5 #列表,查,切片,通过索引(下标),从0开始
 6 # num = ["a","b","c","d","e"]
 7 # print(num[0])#取第一个
 8 # print(num[0:])#取第一个到最后
 9 # print(num[0::2])#取第一个到最后,步长为2
10 # print(num[-2::-2])#倒数第二个往前,步长为2
11 # print(num[3::-2])#从第三个往前,步长为2
12 # b = num[3::-2] #变量接受列表
13 # print(b)#打印变量
14 #
15 # C:\Python37\python_file\venv\Scripts\python.exe C:/Python37/python_file/list_test.py
16 # a
17 # [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
18 # [‘a‘, ‘c‘, ‘e‘]
19 # [‘d‘, ‘b‘]
20 # [‘d‘, ‘b‘]
21 # [‘d‘, ‘b‘]
22 
23 #增加
24 # append :追加
25 # insert:插入
26 # num = ["a","b","c","d","e"]
27 # num.append("t") #将字符串追加到最后的位置
28 # print(num)
29 # num.insert(2,"Y") #将字符串插入到指定的位置
30 # print(num)
31 #
32 # C:\Python37\python_file\venv\Scripts\python.exe C:/Python37/python_file/list_test.py
33 # [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘t‘]
34 # [‘a‘, ‘b‘, ‘Y‘, ‘c‘, ‘d‘, ‘e‘, ‘t‘]
35 
36 #修改
37 # num = ["a","b","c","d","e"]
38 # num[1] = "t"#将索引1的位置修改为"t"
39 # print(num)
40 # num[1:3] = ["Y","Z"] #将索引1到3的位置,修改为“Y”“Z”
41 # print(num)
42 #
43 # C:\Python37\python_file\venv\Scripts\python.exe C:/Python37/python_file/list_test.py
44 # [‘a‘, ‘t‘, ‘c‘, ‘d‘, ‘e‘]
45 # [‘a‘, ‘Y‘, ‘Z‘, ‘d‘, ‘e‘]
46 
47 #删除
48 # clean 清空列表
49 # remove :删除列表中的内容
50 # pop :弹出列表中的内容
51 # num = ["a","b","c","d","e"]
52 # num.remove("c") #将列表中的“c”删除
53 # print(num)
54 # b = num.pop(3) #将列表位置中索引为3的删除
55 # print(num)
56 # print(b)
57 #
58 # C:\Python37\python_file\venv\Scripts\python.exe C:/Python37/python_file/list_test.py
59 # [‘a‘, ‘b‘, ‘d‘, ‘e‘]
60 # [‘a‘, ‘b‘, ‘d‘]
61 # e
62 
63 # del num[3]
64 # print(num) #[‘a‘, ‘b‘, ‘c‘, ‘e‘]
65 # del num
66 # print(num) #NameError: name ‘num‘ is not defined
67 
68 # count 统计某个元素在列表中出现的次数
69 # num = ["a","b","c","d","e","b"]
70 # a = num.count("b") #统计"b"在列表num中的次数
71 # print(a) # 2
72 
73 # extend 将一个列表追加到另一个列表中
74 # a = ["1","2","3"]
75 # b = ["5","6","7"]
76 # a.extend(b) #将b列表追加到a中
77 # print(a) #[‘1‘, ‘2‘, ‘3‘, ‘5‘, ‘6‘, ‘7‘]
78 # print(b)#[‘5‘, ‘6‘, ‘7‘]
79 
80 # index 查询字符串在列表中的索引位置
81 # num = ["a","b","c","d","e","b"]
82 # b = num.index("d") #查询"d"在列表num中的索引位置
83 # print(b) # 3
84 
85 # reverse 将列表中内容的位置倒过来
86 # num = ["a","b","c","d","e","b"]
87 # num.reverse() #将列表中内容位置倒过来
88 # print(num) #[‘b‘, ‘e‘, ‘d‘, ‘c‘, ‘b‘, ‘a‘]
89 
90 # sort 将列表中的内容进行排序
91 # a = ["5","3","8","2","4"]
92 # a.sort(reverse=False) #将列表a中的内容进行从小到大排序,默认为False
93 # print(a) # [‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘8‘]
94 # a.sort(reverse=True) #将列表a中的内容进行从大到小排序
95 # print(a) # [‘8‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘]
96 
97 # num = ["a","b","c","d","e","b"]
98 # num.sort() #将列表num中的内容进行排序
99 # print(num) #[‘a‘, ‘b‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]

 

15、python基础学习-列表

标签:int   date   append   基础   pen   file   error   bsp   开始   

原文地址:https://www.cnblogs.com/hlc-123/p/10924626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!