码迷,mamicode.com
首页 > 其他好文 > 详细

pyhton基础学习《元组、字典的常用操作》

时间:2018-08-30 16:47:26      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:art   序列   ror   rabl   元组   some   items   .com   sel   

一、元组

(1)元组的创建

tu = (11,22,33,)或者tu = tupel((11,22,33))

(2)元组常用的操作

def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
#计数

def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
#索引




二、字典

(1)字典的创建

dic = {"k1":"v1","k2":"v2"} 或者 dic = dict(k1 = "v1",k2 = "v2")

(2)字典的常用操作

def clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
#清空字典
技术分享图片
def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
#复制:浅的复制

def fromkeys(*args, **kwargs): # real signature unknown
""" Returns a new dict with keys from iterable and values equal to value. """
#序列设定KEY,附加值,生成新的字典
技术分享图片

def get(self, k, d=None): # real signature unknown; restored from __doc__
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
#当一个KEY不存在字典中时,返回一个默认直none或设置赋一个新的值,当KEY存在时,不生效

技术分享图片

def items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D‘s items """
#输出字典中的对象
技术分享图片














def keys(self): # real signature unknown; restored from __doc__

""" D.keys() -> a set-like object providing a view on D‘s keys """
#输出字典中的KEY
技术分享图片

def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
#指定删除一个KEY
技术分享图片
def popitem(self): # real signature unknown; restored from __doc__
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
"""
#随机删除一个KEY,并反回删除的KEY

技术分享图片














def setdefault(self, k, d=None): # real signature unknown; restored from __doc__

""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
#给KEY设默认值

def update(self, E=None, **F): # known special case of dict.update
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
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]
"""
#更新字典

技术分享图片


def values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D‘s values """
#输出字典KEY的值
技术分享图片

 

pyhton基础学习《元组、字典的常用操作》

标签:art   序列   ror   rabl   元组   some   items   .com   sel   

原文地址:https://www.cnblogs.com/wings-xu/p/9560596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!