老齐python-基础3(列表)
1、定义一个列表
>>> a = [] #创建一个空列表 >>> type(a) #查看数据类型 <class 'list'> >>> bool(a) #判断非空 False >>> print(a) [] >>> a = ['2',3,'tajzhang',] >>> a ['2', 3, 'tajzhang'] >>> type(a) <class 'list'> >>> bool(a) True >>> print(a) ['2', 3, 'tajzhang']
列表是个筐,什么都能装
>>> b = ["hello",a] >>> b ['hello', ['2', 3, 'tajzhang']]
2、索引和切片
与字符串方式相同
>>> a = ['2','3','python.org'] >>> a[0] '2' >>> a[2] 'python.org' >>> a[:2] ['2', '3'] >>> a[1:] ['3', 'python.org'] >>> a[2][7:13] #两次切片 'org'
>>> a.index('2') #索引
0
>>> a[-1]
'python.org'
>>> a[-3:-1] #从右向左截取
['2', '3']
>>> alst = [1,2,3,4,5,6]
>>> alst[:] #显示列表所有
[1, 2, 3, 4, 5, 6]
>>> alst[::2] #步长为2显示列表
[1, 3, 5]
>>> alst[::1]
[1, 2, 3, 4, 5, 6]
3、反转
编程中比较常用
>>> alst = [1,2,3,4,5,6] >>> alst[::-1] #反转 [6, 5, 4, 3, 2, 1] >>> alst [1, 2, 3, 4, 5, 6] >>> lang ='python' >>> lang[::-1] #字符串同样支持反转 'nohtyp' >>> alst[::-2] [6, 4, 2] >>> list(reversed(alst)) #反转函数 [6, 5, 4, 3, 2, 1] >>> list(reversed("abcd")) ['d', 'c', 'b', 'a']
4、操作列表
4.1基本操作:与字符串操作方式基本相同
len
+
*
in
max()和min()
>>> lst = ['python','java','c++'] >>> len(lst) 3 >>> alst=[1,2,3,4,5,6] >>> lst + alst ['python', 'java', 'c++', 1, 2, 3, 4, 5, 6] >>> lst * 3 ['python', 'java', 'c++', 'python', 'java', 'c++', 'python', 'java', 'c++'] >>> "python" in lst #是否存在 True >>> "c#" in lst False >>> alst = [1,2,3,4,5,6] >>> max(alst) #最大值 6 >>> min(alst) #最小值 1 >>> min(lst) 'c++'
4.2修改列表元素
>>> cities = ["nanjing","zhejiang"] >>> cities[1] = "suzhou" >>> cities ['nanjing', 'suzhou'] >>> cities.append("shanghai") >>> cities ['nanjing', 'suzhou', 'shanghai'] >>> cities[len(cities):] = ["wuxi"] >>> cities ['nanjing', 'suzhou', 'shanghai', 'wuxi']
5、列表常用函数
>>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__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']
常用:append、count、extend、index、insert、pop、remove、reverse、sort
>>> la = [1,2,3] >>> lb = ['tajzhang','python'] >>> la.extend(lb) #列表合并 >>> la [1, 2, 3, 'tajzhang', 'python'] >>> lb ['tajzhang', 'python'] >>> b = "abx" >>> la.extend(b) #将字符串加入列表 >>> la [1, 2, 3, 'tajzhang', 'python', 'a', 'b', 'x']
>>> la = [1,2,3,'a','b','c']
>>> lb = ['qiwair','python']
>>> la[len(la):] = lb #不同实现方法
>>> la
[1, 2, 3, 'a', 'b', 'c', 'qiwair', 'python']
判断对象是否是可迭代的
>>> astr = "python" >>> hasattr(astr,'__iter__') True >>> hasattr(3,'__iter__') False
5.1 append()和extend()区别
>>> lst = [1,2,3] >>> lst.append(["tajzhang","blog"]) >>> lst [1, 2, 3, ['tajzhang', 'blog']] >>> len(lst) 4 >>> lst2 = [1,2,3] >>> lst2.extend(["tajzhang","blog"]) >>> lst2 [1, 2, 3, 'tajzhang', 'blog'] >>> len(lst2) 5
5.2 count 显示列表中元素重复出现次数的方法
>>> la = [1,2,1,1,3] >>> la.count(1) 3
5.3 index 显示元素索引位置,元素不存在就报错
>>> la = [1,2,3,'a','b','c','tajzhang','python'] >>> la.index(3) 2
5.4 inster 任意位置追加元素
>>> all_user = ['tajzhang','python','blog'] >>> all_user.insert(0,'github') >>> all_user
>>> a = [1,2,3] >>> a.insert(9,666) #索引超过最大值则追加到最后 >>> a [1, 2, 3, 666]
5.5 remove和pop
remove存在列表中即删除,不存在列表中报错,建议配合if判断使用
>>> all_user ['github', 'tajzhang', 'python', 'blog'] >>> if "python" in all_user: all_users.remove("python") print(all_user) else: print("'python' is not in all_users")
pop
>>> all_user ['github', 'tajzhang', 'python', 'blog'] >>> all_user.pop() 'blog' >>> all_user ['github', 'tajzhang', 'python'] >>> all_user.pop(1) 'tajzhang' >>> all_user ['github', 'python'] >>> all_user.pop(3) #超出索引报错 Traceback (most recent call last): File "<pyshell#117>", line 1, in <module> all_user.pop(3) IndexError: pop index out of range
5.6 reverse 倒序
>>> a = [3,5,1,6] >>> a.reverse() >>> a [6, 1, 5, 3] >>> a = [1,2,3,4,5] >>> b = reversed(a) >>> b <list_reverseiterator object at 0x1023aee48> >>> list(b) #reverse不能实现反向迭代,可使用reversed实现 [5, 4, 3, 2, 1] >>> a [1, 2, 3, 4, 5] >>> a.reverse()
5.7 sort sorted()
>>> a [1, 2, 3, 4, 5] >>> a.reverse() >>> a = [6,1,5,3] >>> a.sort() >>> a [1, 3, 5, 6] >>> a.sort(reverse=True) #反向排序 >>> a [6, 5, 3, 1] >>> lst = ["python","java","c","pascal","basic"] >>> lst.sort(key=len) #根据key 排序类似excel >>> lst ['c', 'java', 'basic', 'python', 'pascal']
6、比较字符串和列表
6.1相同点
两者都属于序列类型,不管是组成列表的元素,还是组成字符串的字符,都可以从左向右,依次用0,1,2...(-1,-2,3...)这样的方式建立索引,都可以使用切片
6.2区别
最大区别,列表是可以随意修改的,字符串要重新赋值才可以
6.3多维列表
字符串中每个元祖只能是字符类型,列表中可以是任何类型的数据
7、列表和字符串转化
7.1 str.split()
7.2 "[sep]".join(list)
8、更pythonic的多值替换方法
lst2 = ["python",22,22,"python","linux","python","ubuntu"] lst4 = ['python2' if x == 'pyhton' else x for x in lst2] #多值替换 print(lst4) lst3 = list(set(lst2)) print(lst3) lstcache1 = [22,'linux'] lst5 = ['c++' if x in lstcache1 else x for x in lst3] #去除相同元素,多元素替换同一个元素 print(lst5) lstcache2 = {'python':'pythonIC','c++':'c#'} lst6 = [lstcache2[x] if x in lstcache2 else x for x in lst5] #根据字典映射关系替换 print(lst6)