Python 容器API

来源:互联网 发布:万达乐园和迪士尼 知乎 编辑:程序博客网 时间:2024/06/13 19:09

list

class list([iterable])

构造方法

  • 使用一对方括号表示空列表:[]
  • 使用方括号,用逗号分隔项目[a], [a, b, c]
  • 使用列表推导式: [x for x in iterable]
  • 使用类型构造函数:list()或list(iterable)

对象方法

见Python容器

tuple

构造方法

class tuple([iterable])
- 使用一对括号表示空元组:()
- 对单个元组使用尾随逗号:a,或(a,)
- 以逗号分隔项目:a, b, c or (a, b, c)
- 使用tuple()内建:tuple()或tuple(iterable)

对象方法

见Python容器

range

class range(start, stop[, step])

str

构造方法

class str(object=”)
单引号: ‘允许 嵌入 “一对” 双引号’。
双引号: “允许 嵌入 ‘一对’ 单引号”。
三引号:”’三个单引号”’,”“”三个双引号”“”。

对象方法

Python str字符串

Bytes

构造方法

classmethod bytes.fromhex(string)
classmethod bytearray.fromhex(string)
单引号 b’still allows embedded “double” quotes’
双引号: b”still allows embedded ‘single’ quotes”.
三引号: b”’3 single quotes”’, b”“”3 double quotes”“”

set

class set([iterable])
class frozenset([iterable])

dict

class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)

构造方法

>>> 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})>>> a == b == c == d == e----------classmethod fromkeys(seq[, value])

对象方法:

方法 描述 len(d) 返回字典中的项目数量d d[key] 返回字典d中键为key的元素 d[key] = value 设置d[key]的值为value del d[key] 从d中删除d [key] key in d 如果d有键key,则返回True,否则False key not in d not key in d iter(d) 返回字典的键上的一个迭代器。这是iter(d.keys())的快捷方式\ clear() 从字典中删除所有项目 copy() 返回字典的一个浅拷贝 get(key[, default]) 如果 key 在字典里,返回 key 的值,否则返回 default 值。 items() 返回字典项目的新视图((key, value)对) keys() 返回字典的键的新的视图 pop(key[, default]) 如果键在字典中,请将其删除并返回其值 popitem() 从字典中移除并返回任意一个(key, value)对 setdefault(key[, default]) 如果key在字典中,则返回其值。如果没有,则插入值为default的key,并返回default update([other]) 依据other更新词典的键/值对,覆盖现有的键 values() 返回字典的值的新的视图

参考文献:
http://python.usyiyi.cn/translate/python_352/library/stdtypes.html#list

原创粉丝点击