列表:将数值分组到一个结构中,并且通过编号对其进行引用,列表为有序数据结构
字典:结构类型为映射,字典中的值没有特殊的顺序,但对应一个Key,Key可以是数字,字符串,元组,字典是无序
>>> #字典创建 {key1:val1,key2:val2}
>>> phonebook={"zhangsan":"12","lisi":"14","wanger":"15"}
>>> phonebook
{'wanger': '15', 'zhangsan': '12', 'lisi': '14'}
>>> #由列表转换dict
>>> items=[("name","wanger"),("age",18)]
>>> d = dict(items)
>>> d
{'name': 'wanger', 'age': 18}
>>> #访问字典
>>> d['name']
'wanger'
>>> d = dict(name='zhangsan',age=100)
>>> d
{'name': 'zhangsan', 'age': 100}
>>> #字典基本操作
>>> len(d) #字典长度长度len(dict)
2
>>> d['name']
'zhangsan'
>>> d['name']="lisi"
>>> d
{'name': 'lisi', 'age': 100}
>>> del d['age'] #删除字典项del(key)
>>> d
{'name': 'lisi'}
>>> d["age"]=199
>>> d
{'name': 'lisi', 'age': 199}
>>> d[42]=988
>>> d
{42: 988, 'name': 'lisi', 'age': 199}
>>> d[42]
988
>>> 42 in d
True
>>> d.clear()#清空字典
>>> d
{}
>>>
KeyboardInterrupt
>>> d[1]="one"
>>> d[2]="two"
>>> d
{1: 'one', 2: 'two'}
>>> e=d.copy()#字典复制 dict.copy()
>>> e
{1: 'one', 2: 'two'}
>>> d[2]="three"
>>> d
{1: 'one', 2: 'three'}
>>> e
{1: 'one', 2: 'two'}
>>> e[2]="four"
>>> e
{1: 'one', 2: 'four'}
>>> d
{1: 'one', 2: 'three'}
>>> #字典初始化
>>> {}.fromkeys(['name','age'])
{'name': None, 'age': None}
>>> dict.fromkeys(['k1','k2'])
{'k1': None, 'k2': None}
>>> dict.fromkeys(['k1','k2'],1)
{'k1': 1, 'k2': 1}
>>> a = dict.fromkeys(['k1','k2'],1)
>>> 使用get获取字典值或者判断字典的值
>>> a.get("k1")
1
>>> a.get("k3")
>>> print(a.get('k3'))
None
>>> a
>>> #字典转换为list
{'k1': 1, 'k2': 1}
>>> a.items()
dict_items([('k1', 1), ('k2', 1)])
>>> it=a.items()
>>> it
dict_items([('k1', 1), ('k2', 1)])
>>> list(it)
[('k1', 1), ('k2', 1)]
>>> 字典的pop方法
>>> a.pop('k1')
1
>>> a
{'k2': 1}
>>> a.popitem()
('k2', 1)
>>> a
{}
>>>#字典的更新
>>> a['k3']=5
>>> a
{'k3': 5}
>>> b={}
>>> b[2]=5
>>> b
{2: 5}
>>> b['k3']=10
>>> a.update(b)
>>> a
{2: 5, 'k3': 10}
#拿出字典的值
>>> a.values()
dict_values([5, 10])
>>> list(a.values())
[5, 10]
>>>