您的位置:首页 > 编程语言 > Python开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: