Python基本数据类型:
二、元组,关键字tuple,Python 的元组与列表类似,不同之处在于元组的元素不能修改
1.count(value)计算value在元组中出现的次数
>>> t1 = ('tianmao','jingdong','yamaxun','yangpijuan')
>>>> t1.count(2)
0
>>> t1.count('tianmao')
1
>>>
2.index(value)返回返回指定元素的下标
>>> t2 = (1,2,3,4,5,8,9,10,15,55)
>>>> t2.index(3)
2
3.按照下标获取元组元素的方法,以及获取最大值,最小值以及长度(与list相似)
>>> t2 = (1,2,3,4,5,8,9,10,15,55)
>>> t2[2]
3
>>> t2[2:5]
(3, 4, 5)
>>> t2[::5]
(1, 8)
>>> t2[1::2]
(2, 4, 8, 10, 55)
#内置方法
>>> max(t2)
55
>>> min(t2)
1
>>> len(t2)
10
>>>
三、字典,关键字dict,字典值可以是任何的 python 对象,可以是标准的对象,也可以是用户定义的对象,但键值不行。
1.不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住
2.键必须不可变,所以可以用数字,字符串或元组充当,而用列表就不行
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict1["price"]
5888
>>> dict1 = {["productName"]:"iphone","price":8000,"price":5888,"color":"red"} #键值定义为list类型报错
Traceback (most recent call last):
File "<pyshell#124>", line 1, in <module>
dict1 = {["productName"]:"iphone","price":8000,"price":5888,"color":"red"}
TypeError: unhashable type: 'list'
>>> dict1 = {("productName"):"iphone","price":8000,"price":5888,"color":"red"}
>>> dict1[("productName")]
3.clear() D.clear() -> None. Remove all items from D. 清空字典
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict1.clear()
>>> dict1
{}
4.copy() D.copy() -> a shallow copy of D 浅拷贝
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict2 = dict1.copy()
>>> dict2
{'productName': 'iphone', 'price': 5888, 'color': 'red'}
5.keys(),values(),items()分别返回所有的键值、键值所对应的值、返回所有的键值和键值所对应的值
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict1.values()
dict_values(['iphone', 5888, 'red'])
>>> dict1.keys()
dict_keys(['productName', 'price', 'color'])
>>> dict1.items()
dict_items([('productName', 'iphone'), ('price', 5888), ('color', 'red')])
6.fromkeys(iterable, value=None) 函数用于创建一个新字典,以序列中元素做字典的键,value为字典所有键对应的初始值,value默认值为none
>>> t1 = ("productName","price","color")
>>> dict1 = dict.fromkeys(t1)
>>> dict1
{'productName': None, 'price': None, 'color': None}
>>> dict1 = dict.fromkeys(t1,100)
>>> dict1
{'productName': 100, 'price': 100, 'color': 100}
>>>
7.get()获取指定key值所对应的值,其实跟dict[key]获取差不多,唯一的区别是当dict[key]中key值不存在则报错,而get(key)中key为空不会报错
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict1["productName"]
'iphone'
>>> dict1["product"] #key值不存在报错
Traceback (most recent call last):
File "<pyshell#156>", line 1, in <module>
dict1["product"]
KeyError: 'product'
>>> dict1.get("productName")
'iphone'
>>> dict1.get("product")
>>>
8.pop()弹出指定key值所对应的值,并且在字典中删除
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict1.pop("productName")
'iphone'
>>> dict1
{'price': 5888, 'color': 'red'}
>>>
9.popitem()随机弹出键值对,并且在字典中删除
>>> dict1
{'price': 5888, 'color': 'red'}
>>> dict1.popitem()
('color', 'red')
>>> dict1
{'price': 5888}
>>>
10.setdefault(key,value=none)判断键值在字典中是否存在,如果不存在就将键值对添加进去,并且返回值,如果存在就返回字典中的值(value默认值是none)
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict1.setdefault("productName","xiaomi")
'iphone'
>>> dict1.setdefault("product","pingguo")
'pingguo'
>>> dict1.setdefault("countPrice")
>>> dict1
{'productName': 'iphone', 'price': 5888, 'color': 'red', 'product': 'pingguo', 'countPrice': None}
11.update(dict)将字典dict更新到调用方法的字典中
>>> dict1 = {"productName":"iphone","price":8000,"price":5888,"color":"red"}
>>> dict2 = {}
>>> dict2.update(dict1)
>>> dict2
{'productName': 'iphone', 'price': 5888, 'color': 'red'}
>>>