python学习记录 — (7)列表、元组、字典
2018-02-01 22:12
627 查看
ListTupleDictionary.py
# -*- coding: UTF-8 -*- #################################### List列表 #################################### ### 列表基础 lst = ['lua','python',5.1,2.7] print lst[0] ## lua print lst[1:3] ## ['python', 5.1] lst.append('c++') print lst[4] ## c++ print 'The newly added language is {0[4]}'.format(lst) ## The newly added language is c++ del lst[4] print len(lst) ## 4 ### 列表脚本操作符 # 长度 len lst = ['lua','python',5.1,2.7] print len(lst) ## 4 # 组合 + lst += ['c++'] print lst ## ['lua', 'python', 5.1, 2.7, 'c++'] # 重复 * lst = ['lua','python'] print lst*2 ## ['lua', 'python', 'lua', 'python'] # in print 'lua' in lst ## True # 迭代 for x in lst: print x; ## lua/python # 截取[](左闭右开区间) lst = ['lua','python','c++'] print lst[1:] ## ['python', 'c++'] print lst[:1] ## ['lua'] print lst[-1] ## ['lua'] ### 列表函数 # 比较表元素 lst1 ,lst2 = [123,'abc'],[789,'xyz'] print cmp(lst1,lst2) ## -1(<) print cmp(lst2,lst1) ## 1(>) print cmp(lst1+[123,999],lst2) ## -1(<) # 长度 len() # 列表元素中的最大值 print max(lst1) ## abc print max(lst2) ## xyz # 列表元素中的最小值 print min(lst1) ## 123 print min(lst2) ## 789 # 转换为列表 print list('abc') ## ['a', 'b', 'c'] print list(('a','b','c')) ## ['a', 'b', 'c'] ### 列表方法 del lst lst = ['lua','python','c++','c#'] # append() :列表尾添加元素 lst.append('python') print lst ## ['lua', 'python', 'c++', 'c#', 'python'] # count() :某个元素出现的次数 print lst.count('python') ## 2 # extend() :列表尾添加另一个序列 lst.extend([1,2]) print lst ## ['lua', 'python', 'c++', 'c#', 'python', 1, 2] lst.extend('ab') print lst ## ['lua', 'python', 'c++', 'c#', 'python', 1, 2, 'a', 'b'] lst.extend((1,2)) print lst ## ['lua', 'python', 'c++', 'c#', 'python', 1, 2, 'a', 'b', 1, 2] # index :查找索引位置(第一次)# index :查找索引位置(第一次) print lst.index(1) ## 5 print lst.index(1,8,10) ## 9 # insert :插入元素 lst.insert(1,'insert') print lst ## ['lua', 'insert', 'python', 'c++', 'c#', 'python', 1, 2, 'a', 'b', 1, 2] # pop : 移除元素 lst.pop(1) print lst ## ['lua', 'python', 'c++', 'c#', 'python', 1, 2, 'a', 'b', 1, 2] for i in range(0,7,1): lst.pop() print lst ## ['lua', 'python', 'c++', 'c#'] # remove() :移除元素 lst.remove('c#') print lst ## ['lua', 'python', 'c++'] # reverse() : 反转元素 lst.reverse() print lst ## ['c++', 'python', 'lua'] # sort() : 排序 lst.sort() print lst ## ['c++', 'lua', 'python'] #################################### Tuple元祖 #################################### ## 元祖基础(除了不可更改,其他都跟list一样) tup = ('lua','python',5.1,2.7) ## 元祖脚本操作符(同list) ## 元祖函数(同list) #################################### Dictionary字典 #################################### ## 字典基础 dic = {'name1':'python','name2':'lua','version1':2.7,'version2':5.1,1:2} print dic['name1'] + str(dic['version1']) ## python2.7 print 'The version of language {name1} is {version1}'.format(**dic) ## The version of language python is 2.7 dic['version1'] = 3.0 print 'name1 : {k} | version1 : {v}'.format(k=dic['name1'],v=dic['version1']) ## name1 : python | version1 : 3.0 print len(dic) ## 5 del dic[1] print len(dic) ## 4 ''' 《补充》 1. 键不可重复出现; 2. 值可以没有限制地取任何python对象;(包括用户定义的) 3. 键不可变;(数字、字符串、元组) ''' ## 字典方法 del dic dic = {'k1':'v1','k2':'v2','k3':'v3','k4':'v4','k5':'v5'} # clear() :删除所有元素 dic.clear() print len(dic) ## 0 # copy() :复制字典 dic = {'k1':'v1','k2':'v2','k3':'v3','k4':'v4','k5':'v5'} dic2 = dic.copy() dic2['k1']=2 print dic2['k2'],id(dic2) ## v2 56273632 print dic['k1'],id(dic) ## v1 124854752 # fromkeys() :通过序列设置键 dic2= dict.fromkeys(['key1','key2','key3'],0) print dic2 ## {'key3': 0, 'key2': 0, 'key1': 0} # get() : 获取指定键的值 print dic.get('k6') ## None print dic.get('k6','new v6') ## new v6 print dic.get('k5') ## v5 # has_key() :是否包含键 print dic.has_key('k5') ## True print dic.has_key('k6') ## False # items() : 返回可遍历的(键,值)元组数组 print dic.items() ## [('k3', 'v3'), ('k2', 'v2'), ('k1', 'v1'), ('k5', 'v5'), ('k4', 'v4')] # keys() :返回key的列表 print dic.keys() ## ['k3', 'k2', 'k1', 'k5', 'k4'] # setdefault(k,default) : 设置键值对(存在则不变,不存在则按default值设置) dic.setdefault('k6','v6') print dic['k6'] ## v6 dic.setdefault('k5','new v5') print dic['k5'] ## v5 # update(dic2) : 把字典dict2的键/值更新进来 del dic del dic2 dic = {'k1':'v1','k2':'v2'} dic2 = {'k2':'new v2','k3':'v3'} dic.update(dic2) print dic ## {'k3': 'v3', 'k2': 'new v2', 'k1': 'v1'} # valus :返回value的列表 print dic.values() ## ['v3', 'new v2', 'v1'] # pop(k,default) :删除指定键的值,并返回;(无则返回default) print dic.pop('k1') ## v1 print dic.pop('k4','没有') ## 没有 # popitem() :随机pop出一个键值对 print len(dic) ## 2 print dic.popitem(),len(dic) ## ('k3', 'v3') 1
相关文章推荐
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/
- 【每日一记3.16】python学习记录3----字典,列表,元组
- Python3.5入门学习记录-列表、元组、字典
- Python学习--识别“字符串&字典&列表&元组“
- 列表、元组、字典、引用的一些操作(数据结构)--python学习(3)
- Python 学习 (一) python的基本数据类型 元组 列表 字典
- Python学习笔记:列表,元组和字典
- Python学习笔记———列表(list)、元组(tuple)、字典(dict)
- Python学习笔记 之元组、列表及字典
- Python中列表、字典、元组数据结构的简单学习笔记
- python学习总结---列表、元组、字符串、字典中一些常用函数
- python核心编程学习记录之序列(字符串元组列表)
- Python学习笔记二:列表、字典、元组、集合 简介
- Python简单学习(lecture2)数字、字符串、列表、元组、字典
- python学习三(列表、元组、字典)
- python开发学习-day02(元组、字符串、列表、字典深入)
- python学习笔记之列表、元组、字典(1)
- Python学习笔记(9)-列表list、元组tuple、字典dict、集合set
- Python 入门学习 -----变量及基础类型(元组,列表,字典,集合)
- Python学习笔记六:列表、元组、字典