Python数据系列二字典Dic

本系列要对Python在数据处理中经常用的列表(list)、元组(tuple)、字典(dictionary)、array(数组)-numpy、DataFrame-pandas、集合(set)等数据形式的特征、常用操作进行详述。

今天,开启本系列的第二篇文章---Python数据系列(二)-字典Dictionary:Python的“大胃王”。

字典:用“{}”标识,又索引(key)和它对应的值(value)组成,键值为键值对。它是除列表以外,python中最灵活的内置数据结构类型。类似于其他语言的k-v数据类型。

字典具有如下的特点:

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key=value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中,格式如下所示:d={key1:value1,key2:value2}

键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。

字典中的元素是通过key关键字来存取的,而不是通过偏移存取的。

字典是无序的对象组合,列表时有序的对象组合。

字典可以嵌套,也就是字典里面嵌套字典,可多层嵌套。

字典的创建、修改、添加、清空、删除

In[1]:dict={"class":"5","name":"莉莉","年龄":"12"}In[2]:dictOut[2]:{class:5,name:莉莉,年龄:12}In[3]:dict["name"]#取对应键的值Out[3]:莉莉In[4]:dict["class"]="6"#给键赋予新值In[5]:dict["年龄"]="7"In[6]:dictOut[6]:{class:6,name:莉莉,年龄:7}In[7]:dict["年龄"]="13"In[8]:dictOut[8]:{class:6,name:莉莉,年龄:13}In[9]:dict["father"]="wangxiao"#给字典增加新的键值对In[10]:dictOut[10]:{class:6,father:wangxiao,name:莉莉,年龄:13}In[11]:deldict["father"]#删除字典内指定键值对In[12]:dictOut[12]:{class:6,name:莉莉,年龄:13}In[13]:dict.clear()#清空字典In[14]:dictOut[14]:{}In[15]:deldict#删除字典In[16]:dictOut[16]:dict

字典的常用操作:

In[1]:dict={"class":"5","name":"莉莉","年龄":"12"}In[2]:len(dict)#计算字典元素个数,即键的总数Out[2]:3In[3]:str(dict)#将值转化为适于人阅读的形式,以可打印的字符串表示Out[3]:"{class:5,年龄:12,name:莉莉}"In[4]:dictOut[4]:{class:5,name:莉莉,年龄:12}In[5]:forkey,valueindict.items():#遍历字典的键、值...:print(key)...:print(value)...:class5年龄12name莉莉In[6]:dict.get("class")#获取对应键的值,和dict["class"]类似Out[6]:5In[7]:dict.has_key("class")#判定字典内是否有该键值对---------------------------------------------------------------------------AttributeErrorTraceback(mostrecentcalllast)ipython-input-7-acf5f8be5inmodule()----1dict.has_key("class")AttributeError:dictobjecthasnoattributehas_keyIn[8]:dict.__contains__("class")#Python3.X里不包含has_key()函数,被__contains__(key)替代Out[8]:TrueIn[9]:dict.items()#字典内内容Out[9]:dict_items([(class,5),(年龄,12),(name,莉莉)])In[10]:dict.keys()#取字典内的键Out[10]:dict_keys([class,年龄,name])In[11]:dict2={"mother":"feifei"}In[12]:dict.update(dict2)#将另一个字典与该字典融合In[13]:dictOut[13]:{class:5,mother:feifei,name:莉莉,年龄:12}In[14]:dict.valuesOut[14]:functiondict.valuesIn[15]:dict.values()#取字典内的值Out[15]:dict_values([5,feifei,12,莉莉])In[16]:dict.pop("mother")#删除字典内指定键值对,返回对应键的值Out[16]:feifeiIn[17]:dictOut[17]:{class:5,name:莉莉,年龄:12}

字典的对比,由于python版本的升级,存在的问题

In[1]:dict={"class":"5","name":"莉莉","年龄":"12"}In[2]:dict1={"class":"5","name":"莉莉","年龄":"12"}In[3]:cmp(dict,dict1)#比较两个字典---------------------------------------------------------------------------NameErrorTraceback(mostrecentcalllast)ipython-input-3-3ea9fcinmodule()----1cmp(dict,dict1)NameError:namecmpisnotdefined#python3.4.3的版本中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块。In[4]:importoperatorIn[5]:operator.eq(dict,dict1)Out[5]:TrueIn[6]:dict1={"class":"5","name":"莉莉","年龄":"15"}In[7]:operator.eq(dict,dict1)Out[7]:FalseIn[8]:

上一篇文章Python数据系列(一)-列表List:Python的“苦力”得到了很多小伙伴的反馈,说到list的copy和deepcopy,这块的内容由于在数据的多个形式都出现过,拷贝部分在后期文章专门进行介绍。

本期对字典dict的基本操作,大体上包含了在日常学习工作中最常用到的操作形式。希望上述的整理能够对你接下来的学习有所帮助。更多关于Python的操作,可进入



转载请注明地址:http://www.sanbaicaoasb.com/scgj/8490.html
  • 上一篇文章:
  • 下一篇文章: 没有了
  • 热点文章

    • 没有热点文章

    推荐文章

    • 没有推荐文章