python 数据结构

来源:互联网 发布:centos的vmdk文件下载 编辑:程序博客网 时间:2024/04/30 17:54

集合:

Python 还包含了一个数据类型 set (集合)。集合是一个无序不重复元素的集。基本功能包括关系测试和消除重复元素。集合对象还支持 union(联合),intersection(交),difference(差)和 sysmmetric difference(对称差集)等数学运算。

大括号或 set() 函数可以用来创建集合。注意:想要创建空集合,你必须使用 set() 而不是 {}。后者用于创建空字典。

#!/usr/bin/env python2.7# -*- coding: utf-8 -*-'''Created on 2016/4/15File Name:py_set.pyauthor: LindenTaoDescription : set(集合)'''basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']fruit = set(basket)     # create a set without duplicatesprint(fruit)print('orange' in fruit)    # fast membership testingprint('crabgrass' in fruit)# Demonstrate set operations on unique letters from two wordsa = set('abracadabra')b = set('alacazam')print(a,b)      # unique letters in a, b# 差补/相对补集( - ),等价方法:difference(),letters in a but not in bprint('a - b : ', a - b, a.difference(b))# 联合( | ),等价方法:union(),letters in either a or bprint('a | b : ', a | b, a.union(b))# 交集( & ),等价方法:intersection(),letters in both a and bprint('a & b : ', a & b, a.intersection())# 对称差分( ^ ),等价方法:symmetric_difference,letters in a or b but not bothprint('a ^ b : ', a ^ b, a.symmetric_difference(b))

执行结果:

set(['orange', 'pear', 'apple', 'banana'])TrueFalse(set(['a', 'r', 'b', 'c', 'd']), set(['a', 'c', 'z', 'm', 'l']))('a - b : ', set(['r', 'b', 'd']), set(['r', 'b', 'd']))('a | b : ', set(['a', 'c', 'b', 'd', 'm', 'l', 'r', 'z']), set(['a', 'c', 'b', 'd', 'm', 'l', 'r', 'z']))('a & b : ', set(['a', 'c']), set(['a', 'c', 'r', 'b', 'd']))('a ^ b : ', set(['b', 'd', 'm', 'l', 'r', 'z']), set(['b', 'd', 'm', 'l', 'r', 'z']))

字典:

Python 内建数据类型是字典。字典在某些语言中可能称为 联合内存 (associative memories) 或 联合数组 (associative arrays)。序列是以连续的整数为索引,与此不同的是,字典以 关键字 为索引,关键字可以是任意不可变类型,通常用字符串或数值。如果元组中只包含字符串和数字,它可以作为关键字,如果它直接或间接地包含了可变对象,就不能当做关键字。不能用链表做关键字,因为链表可以用索引、切割或者 append() 和 extend() 等方法改变。

理解字典的最佳方式是把它看做无序的键:值对 (key:value 对)集合,键必须是互不相同的(在同一个字典之内)。一对大括号创建一个空的字典:{}。初始化链表时,在大括号内放置一组逗号分隔的键:值对,这也是字典输出的方式。

字典的主要操作是依据键来存储和析取值。也可以用 del 来删除键:值对(key:value)。如果你用一个已经存在的关键字存储值,以前为该关键字分配的值就会被遗忘。试图从一个不存在的键中取值会导致错误。

对一个字典执行 keys() 将返回一个字典中所有关键字组成的无序列表(如果你想要排序,只需使用 sorted())。使用 in 关键字(指 Python 语法)可以检查字典中是否存在某个关键字(指字典)。

class dict(**kwarg)class dict(mapping, **kwarg)class dict(iterable, **kwarg)    Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.get(key[, default])    Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.pop(key[, default])    If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised. values()    Return a copy of the dictionary’s list of values.返回字典中的所有值

示例代码:

#!/usr/bin/env python2.7# -*- coding: utf-8 -*-'''Created on 2016/4/15File Name:py_dict.pyauthor: LindenTaoDescription : dict(字典)'''a = dict(one=1, two=2, three=3)b = {'one': 1, 'two': 2, 'three': 3}c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))d = dict([('two', 2), ('one', 1), ('three', 3)])e = dict({'three': 3, 'one': 1, 'two': 2})print(a == b == c == d == e)# 执行结果:True

参考链接:

The Python Standard Library:https://docs.python.org/2.7/library/index.html
Python Tutorial:https://docs.python.org/2/tutorial/index.html
Python入门指南:http://www.pythondoc.com/pythontutorial27/index.html

0 0