• python的方法总结:


    1. Python的字典的items(), keys(), values()都返回一个list

    >>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'world' }  
    >>> dict.values()  
    ['b', 2, 'world']  
    >>> dict.keys()  
    ['a', 1, 'hello']  
    >>> dict.items()  
    [('a', 'b'), (1, 2), ('hello', 'world')]  
    >>>  

    2. 在Python中用keys()方法返回字典键

    In [1]: dicts = {'a':1,'b':2,'c':3,'d':4}
    
    In [2]: dicts
    Out[2]: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    
    In [3]: list1 = dicts.keys()
    
    In [4]: list1
    Out[4]: ['a', 'c', 'b', 'd']
    
    In [5]: list2 = dicts.values()
    
    In [6]: list2
    Out[6]: [1, 3, 2, 4]
    
    In [7]: 

    综上所述:keys()方法返回字典的"键"values()方法返回字典的"值"

    3. 名称前的单下划线(如:_shahriar)

    程序员使用名称前的单下划线,用于指定该名称属性为“私有”。这有点类似于惯例,为了使其他人(或你自己)使用这些代码时将会知道以“_”开头的名称只供内部使用。正如Python文档中所述:

    以下划线“_”为前缀的名称(如_spam)应该被视为API中非公开的部分(不管是函数、方法还是数据成员)。此时,应该将它们看作是一种实现细节,在修改它们时无需对外部通知。

    正如上面所说,这确实类似一种惯例,因为它对解释器来说确实有一定的意义,如果你写了代码“from <模块/包名> import *”,那么以“_”开头的名称都不会被导入,除非模块或包中的“__all__”列表显式地包含了它们.

    4. 名称前的双下划线(如:__shahriar)

    名称(具体为一个方法名)前双下划线(__)的用法并不是一种惯例,对解释器来说它有特定的意义。Python中的这种用法是为了避免与子类定义的名称冲突。Python文档指出,“__spam”这种形式(至少两个前导下划线,最多一个后续下划线)的任何标识符将会被“_classname__spam”这种形式原文取代,在这里“classname”是去掉前导下划线的当前类名。例如下面的例子:

    >>> class A(object):
    ... def _internal_use(self):
    ... pass
    ... def __method_name(self):
    ... pass
    ...
    >>> dir(A())
    ['_A__method_name', ..., '_internal_use']

    5. python find()用法

    1.
    str = "01213456" if str.find("23"): print "YES!" else: print "NO!"
    2. 
    str = "01213456"
    
    if str.find("23"):
        print "YES!"
    else:
        print  "NO!"

    上两个案例结果都为“YES!”, 非常令我吃惊,2不应该是NO!吗?

    这里注意两点

         1. if  str.find('23'):  此时默认为  str.find('23') != 0:

         2. find()函数找不到时返回为-1

    经查阅得知其用法

    函数原型:find(str, pos_start, pos_end)

    解释:

    • str:被查找“字串”
    • pos_start:查找的首字母位置(从0开始计数。默认:0)
    • pos_end: 查找的末尾位置(默认-1)

    返回值:如果查到:返回查找的第一个出现的位置。否则,返回-1

        1.str = "0123"
                         print str.find("2",1,-1)      #2
    
               2.str = "1111"
                         print str.find("1")           #0,首次出现的位置

     6. python的decode()方法:

    decode()方法使用注册编码的编解码器的字符串进行解码。它默认为默认的字符串编码。

    语法

    以下是decode()方法的语法:

    str.decode(encoding='UTF-8',errors='strict')

    参数

    • encoding -- 这是所使用的编码。对于所有的编码方案的列表,请访问:标准编码库

    • errors -- 这可能是给定一个不同的错误处理机制。默认的错误是“严格”,即编码错误提出UnicodeError。其他可能的值是ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 并通过codecs.register_error().注册的任何其他名称。

    返回值

    此方法返回的字符串的解码版本。

    例子

    下面的例子显示了decode()方法的使用。

    #!/usr/bin/python
    
    str = "this is string example....wow!!!";
    str = str.encode('base64','strict');
    
    print "Encoded String: " + str;
    print "Decoded String: " + str.decode('base64','strict')
     当我们运行上面的程序,它会产生以下结果:
    
    Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
    
    Decoded String: this is string example....wow!!!
  • 相关阅读:
    新浪微博 js 解密
    新浪微博、qq rsa密码加密c#实现
    C#版本的discuz authcode函数
    搬地方了,在github弄了个新博客
    python 发送邮件
    通用网页广告监测,ADBlock plus算法的C#实现。
    58同城登录 c#,非直接操作js
    python模块之smtplib: 用python发送SSL/TLS安全邮件
    Python少打字小技巧
    python模块之poplib: 用pop3收取邮件
  • 原文地址:https://www.cnblogs.com/blogofwyl/p/4607824.html
Copyright © 2020-2023  润新知