python对象与类型之映射(dict)与集合(set)

一、映射(dict)

(1)定义
dict() # new empty dictionary
dict(mapping) # new dictionary initialized from a mapping object's (key, value) pairs
a = (1,2,3);b = ('a','b','c')
dict(zip(b,a)) # {'a': 1, 'b': 2, 'c': 3}
dict(iterable) -> new dictionary initialized as if via:
      d = {}
      for k, v in iterable:
          d[k] = v
 dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword
 argument list.   
 dict(a=1, b=2) # {'a': 1, 'b': 2}

注意:
(1)映射类型表示一个任意对象的集合,而且可以通过另一个几乎是任意键值的集合进行索引。
(2)和序列不同,映射对象是无序的,可以通过数字、字符串和其他对象进行索引。
(3)映射是可变的。
(4)任意不可变对象都可以用作字典键值,如字符串、数字、元组等。包含可变对象的列表、字典和元组不能用作键,因为字典要求键值保持不变,可以通过hash函数检查一个对象是否可以哈希化,即是否可以用作字典的键。
(5)可以使用键索引运算符m[k],其中k是一个键值。

(2)方法及属性
dict.clear()Remove all items from D.
dict.copy()a shallow copy(浅复制) of D
dict.get(key,default=None)Return the value for key if key is in the dictionary, else default.(返回value或者default对应的值)
dict.items()a set-like (类似于集合的)object providing a view on Dict’s items(条目)
dict.keys()a set-like object providing a view on Dict’s keys(返回键)
dict.pop(k[,d])remove specified key and return the corresponding value. If key is not found, d(任意对象) is returned if given, otherwise KeyError is raised.(删除指定键项)
dict.popitem()没有参数。remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.(删除随机的一项)
dict.setdefault(key, default=None)Insert key with a value of default if key is not in the dictionary.(插入键值)Return the value for key if key is in the dictionary, else default.
dict.update([E, ]**F)Update D from dict/iterable E and F. (将E,F中所有项加入d中);If E is present and has a .keys() method,(字典) then does: for k in E: D[k] = E[k]; If E is present and lacks a .keys() method, (非字典)then does: for k, v in E: D[k] = v, In either case, this is followed by: for k in F: D[k] = F[k]
dict.values()an object providing a view on D’s values(返回值)

二、集合(set、frozenset)

(1)定义

集合是唯一项的无序集
与序列不同,集合不提供索引和切片操作(用 [ ] 进行)。
和字典也有所区别,即对象不存在相关的键值。
放入集合的项目必须是不可变的。
集合分为可变集合(set)以及不可变集合(frozenset)。

(2)set方法及属性
set.add()Add an element to a set.This has no effect if the element is already present.(添加)
set.copy()Return a shallow copy of a set. (浅复制)
set.clear()Remove all elements from this set.(清除)
set.difference()Return the difference of two or more sets as a new set.(多个集合的差集)
set.difference_update()Remove all elements of another set from this set.(删除与另外一个集合的交集)
set.discard()Remove an element from a set if it is a member.(删除特定元素)If the element is not a member, do nothing.
set.intersection()Return the intersection of two sets as a new set.(返回两个集合的交集)
set.intersection_update()Update a set with the intersection of itself and another.
set.isdisjoint()Return True if two sets have a null intersection.(是否无交集)
set.issubset()Report whether another set contains this set. (是否子集)
set.issuperset()Report whether this set contains another set. (是否包含另一个集合)
set.pop()Remove and return an arbitrary(任意的) set element.Raises KeyError if the set is empty.
set. remove()Remove an element from a set; it must(必须) be a member. If the element is not a member, raise a KeyError.(删除)
set.symmetric_difference(t)Return the symmetric difference(对称差集) of two sets as a new set.(返回在s或者t中,但是不同时在s和t中的元素)
set.symmetric_difference_update(…)Update a set with the symmetric difference of itself and another.
set.union()Return the union(并集) of sets as a new set.
set.update(itterable)Update a set with the union(并集) of itself and others.(更新)
(3)frozenset方法及属性
f = frozenset((1,2,3,4,7))
f1 = frozenset((1,3,5,7))
f.copy()Return a shallow copy of set f.(结果为 frozenset({1, 2, 3, 4, 7})
f.difference(f1)Return the difference of two or more sets as a new set.(结果为 frozenset({2, 4}))
f.intersection(f1)Return the intersection of two sets as a new set.(结果为 frozenset({1, 3, 7}))
f.isdisjoint(f1)Return True if two sets have a null intersection. (结果为 False)
f.issubset(f1)Report whether another set contains this set. (结果为 False)
f.issuperset(f1)Report whether this set contains another set.(结果为 False)
f.symmetric_difference(f1)Return the symmetric difference of two sets as a new set.(结果为 frozenset({2, 4, 5}))
f.union(f1)Return the union of sets as a new set. (结果为 frozenset({1, 2, 3, 4, 5, 7}))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值