Python之列表,元祖和字典
1.列表是可变的,可以改变的序列,它能够保存任何数据类型
列表中需要注意的内容几几个函数说明请见代码注释部分:
#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time : 2017/10/23 9:36
# @Author : hantong
# @File : list.py
a ='qerfu89kk9iktrff'
print(type(a))
b = list(a)
print(type(b))
# append 追加:在列表末尾追加
print('*append*'*10)
b.append('acc')
#在末尾追加acc字符串
print(b)
# index 符合条件的list下标
print('*index*'* 10)
print(b.index('acc'))
#查看某个字符串的下标
#insert 插入字符
print('*insert*'*10)
b.insert(1,'hehe')
#在下标1的字符串前插入hehe
print(b)
#pop 末尾删除
print('*pop*'*10)
b.pop()
print(b)
#末尾的acc已经被删除
#remove 删除指定元素
print('*remove*'*10)
b.remove('8')
print(b)
#元素8已经被删除
#reverse 对list进行倒序排列
print('*reverse*'*10)
b.reverse()
print(b)
#列表b已经按照倒叙重新排列
#sort 对list进行排序
print('*sort*'*10)
b.sort()
print(b)
#切片
# print('*[:]*'*15)
print(b[:])
# #所有元素
print(b[3:])
#切出从第三个以后的所有元素,就是除了下标分别是0,1,2的元素
print(b[4:])
#切出从第四个以后的所有元素,就是除了下标分别是0,1,2,3的元素
print(b[3:7])
#切出从第三到第六个所有元素,就是下标分别是3,4,5,6的元素
print(b[3:8:2])
#切出下标从3到8,并且步长2的所有元素
执行结果如下:
C:\Python27\python.exe E:/迁移资料/python/pycharm2017pjb/PycharmProjects/list.py
<type 'str'>
<type 'list'>
*append**append**append**append**append**append**append**append**append**append*
['q', 'e', 'r', 'f', 'u', '8', '9', 'k', 'k', '9', 'i', 'k', 't', 'r', 'f', 'f', 'acc']
*index**index**index**index**index**index**index**index**index**index*
16
*insert**insert**insert**insert**insert**insert**insert**insert**insert**insert*
['q', 'hehe', 'e', 'r', 'f', 'u', '8', '9', 'k', 'k', '9', 'i', 'k', 't', 'r', 'f', 'f', 'acc']
*pop**pop**pop**pop**pop**pop**pop**pop**pop**pop*
['q', 'hehe', 'e', 'r', 'f', 'u', '8', '9', 'k', 'k', '9', 'i', 'k', 't', 'r', 'f', 'f']
*remove**remove**remove**remove**remove**remove**remove**remove**remove**remove*
['q', 'hehe', 'e', 'r', 'f', 'u', '9', 'k', 'k', '9', 'i', 'k', 't', 'r', 'f', 'f']
*reverse**reverse**reverse**reverse**reverse**reverse**reverse**reverse**reverse**reverse*
['f', 'f', 'r', 't', 'k', 'i', '9', 'k', 'k', '9', 'u', 'f', 'r', 'e', 'hehe', 'q']
*sort**sort**sort**sort**sort**sort**sort**sort**sort**sort*
['9', '9', 'e', 'f', 'f', 'f', 'hehe', 'i', 'k', 'k', 'k', 'q', 'r', 'r', 't', 'u']
['9', '9', 'e', 'f', 'f', 'f', 'hehe', 'i', 'k', 'k', 'k', 'q', 'r', 'r', 't', 'u']
['f', 'f', 'f', 'hehe', 'i', 'k', 'k', 'k', 'q', 'r', 'r', 't', 'u']
['f', 'f', 'hehe', 'i', 'k', 'k', 'k', 'q', 'r', 'r', 't', 'u']
['f', 'f', 'f', 'hehe']
['f', 'f', 'i']
进程已结束,退出代码0
2.元祖:Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组创建很简单,只 需要在括号中添加元素,并使用逗号隔开即可
详细说明请见代码注释部分:
#!/usr/bin/env python # -*- coding:utf8 -*- # @Time : 2017/10/23 11:37 # @Author : hantong # @File : tuple.py # 本节课讲解tuple元组 a = ('q','s','r','s','f','s',123,'r') print(type(a)) #可以看到a的类型是元组 #count 统计元素在元组中的数量 print('*count*'*10) print(a.count('s')) #可以看到统计s的出现次数是3次 #index 查看某个元素的下标 print('*index*'*10) print(a.index('s'))执行结果如下:
C:\Python27\python.exe E:/迁移资料/python/pycharm2017pjb/PycharmProjects/tuple.py
<type 'tuple'>
*count**count**count**count**count**count**count**count**count**count*
3
*index**index**index**index**index**index**index**index**index**index*
1
进程已结束,退出代码0
3.字典:Python中唯一内建的映射类型。字典中的值并没有特殊的顺序,但是都存储在一个特定的键(Key)里。
键可以是数字、字符串甚至是元组。
详细说明请见代码注释部分:
#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time : 2017/10/23 14:45
# @Author : hantong
# @File : dict.py
k = {'a':1,'b':2}
print(k)
print(type(k))
k1 = dict(a = 1,b = 2,c = 3)
print(k1)
print(type(k1))
#字典中常用的方法
#get
print(k1.get('a'))
#keys 获取keys值
print(k1.keys())
#values 获取value值
print(k1.values())
#iteritems 返回的结果是一个迭代器
#items 返回的结果是一个列表
print(k1.items())
print(k1.iteritems())
for x,y in k.iteritems():
print(x,y)
#fromkeys 生成字典
l = ('a','b','c','d')
m = {}
n = m.fromkeys(l,666)
# n = dict.fromkeys(l,666)
print(n)
#生成字典n
#zip 函数,合并两个列表生成一个对象
l1 = ('a','b','c','d')
l2 = (4,3,2,1,)
dict = dict(zip(l1,l2))
print(dict)
#这样即可把两个list生成一个字典
#update 把一个字典的数据添加另外一个字典里面
f = {'sex':'man'}
dict.update(f)
print(dict)
#生成新的dict字典
执行结果如下:
C:\Python27\python.exe E:/迁移资料/python/pycharm2017pjb/PycharmProjects/dict.py
{'a': 1, 'b': 2}
<type 'dict'>
{'a': 1, 'c': 3, 'b': 2}
<type 'dict'>
1
['a', 'c', 'b']
[1, 3, 2]
[('a', 1), ('c', 3), ('b', 2)]
<dictionary-itemiterator object at 0x00000000026680E8>
('a', 1)
('b', 2)
{'a': 666, 'c': 666, 'b': 666, 'd': 666}
{'a': 4, 'c': 2, 'b': 3, 'd': 1}
{'a': 4, 'c': 2, 'b': 3, 'd': 1, 'sex': 'man'}
进程已结束,退出代码0
字典综合训练:
#!/usr/bin/env python # -*- coding:utf8 -*- # @Time : 2018/3/31 22:04 # @Author : hantong # @File : 20180331.py m={"name":"hanson","age":"32","address":"shanghai"} print(type(m)) print(m.get("name")) #获取字典的key对应的value值 n=m.copy() print(n) print(m.pop("name")) #删除字典中key和value值 print(m) print(m.items()) mm=m.items() #返回的是列表 print(type(mm)) dd={} d=dd.fromkeys(m,888) #生成新的字典 print(d) l1 = ['a','b','c','d'] l2 = ['4','3','2','1'] #zip可以爸两个列表合并转转成新的字典 dict = dict(zip(l1,l2)) print(dict) f = {'sex':'man'} dict.update(f) #把一个字典的数据合并到另外一个字典 print(dict)
执行结果如下:
<class 'dict'>
hanson
{'name': 'hanson', 'age': '32', 'address': 'shanghai'}
hanson
{'age': '32', 'address': 'shanghai'}
dict_items([('age', '32'), ('address', 'shanghai')])
<class 'dict_items'>
{'age': 888, 'address': 888}
{'a': '4', 'b': '3', 'c': '2', 'd': '1'}
{'a': '4', 'b': '3', 'c': '2', 'd': '1', 'sex': 'man'}
内置函数isinstance
语法:
isinstance(object, classinfo) 前面跟要判断的对象,后面跟数据类型(比如:int等),返回bool值
print(isinstance(0,int))
print(isinstance(0,float))
执行结果:
True
False
hasattr函数:
语法:
hasattr(object,name) 判断一个对象属性是否有name属性或者name方法,返回bool值
实例:
1 >>> class test(): 2 ... name="xiaohua" 3 ... def run(self): 4 ... return "HelloWord" 5 ... 6 >>> t=test() 7 >>> hasattr(t, "name") #判断对象有name属性 8 True 9 >>> hasattr(t, "run") #判断对象有run方法 10 True 11 >>>