• python基础技巧综合训练题2


    1,判断一个字符串中的每一个字母是否都在另一个字符串中,可以利用集合的特性来解,集合的元素如果存在,再次更新(update) 是添加不进集合的,那么集合的长度还是跟原来一样,如果添加进去,集合长度就会增加

    >>> a = 'ghost'
    >>> b = 'hello, can you help me install ghost windows xp system'
    >>> b_set = set( b )
    >>> b_set.update( list( a ) )
    >>> print len( b_set ) == len( set( b ) )
    True
    >>> a = 'abcostg'
    >>> b_set.update( list( a ) )
    >>> print len( b_set ) == len( set( b ) )
    False
    >>> 

    2,如果是多个字符呢?

    #!/usr/bin/python
    #coding:utf-8
    
    #str_list = [ 'abc', 'ghost', 'hello' ]
    str_list = [ 'abc', 'ghost', 'hellox' ]
    target_str = "abcdefghijklopqrst"
    target_str_set = set( target_str )
    
    for val in str_list:
        target_str_set.update( val )
    
    print len( target_str_set ) == len( set( target_str ) )

    3,统计出现次数最多的字符

    ghostwu@ghostwu:~/python/tmp$ python str3.py
    [('f', 7), ('s', 5), ('a', 4), ('j', 4), ('k', 3), ('h', 2), ('3', 2), ('1', 2), ('2', 2), ('d', 1), ('l', 1), ('4', 1), (';', 1)]
    ghostwu@ghostwu:~/python/tmp$ cat str3.py 
    #!/usr/bin/python
    #coding:utf-8
    
    str = 'askfjkjasf1234fasdfasfsh;lkjfhjf123'
    
    l = ( [ ( key, str.count( key ) ) for key in set( str ) ] )
    l.sort( key = lambda item : item[1], reverse = True )
    print l
    
    ghostwu@ghostwu:~/python/tmp$ 

    这里有个lambda表达式, key指定按哪个键排序, item是形参,代表当前的元组,item[1],那就是取元组中第2项,这里就是字符串的次数,reverse = True,从高到低排序 .

    4,统计this模块中, be, is, than,三个单词的出现次数

    ghostwu@ghostwu:~/python/tmp$ !p
    python statics.py 
    [('be', 3), ('is', 10), ('than', 8)]
    ghostwu@ghostwu:~/python/tmp$ cat statics.py 
    #!/usr/bin/python
    #coding:utf-8
    
    import os
    this_str = os.popen( "python -m this" ).read()
    this_str = this_str.replace( '
    ', '' )
    l = this_str.split( ' ' )
    
    print [ ( x, l.count( x ) ) for x in ['be', 'is', 'than' ] ]
    ghostwu@ghostwu:~/python/tmp$ 

    os.popen( "python -m this" ).read  读出命令行python -m this  模块的执行结果到一个字符串中

    5,用位移运算符,换算b, kb, mb之间的转换关系

    ghostwu@ghostwu:~/software$ ls -l sogoupinyin_2.2.0.0102_amd64.deb 
    -rw-rw-r-- 1 ghostwu ghostwu 22852956 2月   2 14:36 sogoupinyin_2.2.0.0102_amd64.deb
    ghostwu@ghostwu:~/software$ ls -lh sogoupinyin_2.2.0.0102_amd64.deb 
    -rw-rw-r-- 1 ghostwu ghostwu 22M 2月   2 14:36 sogoupinyin_2.2.0.0102_amd64.deb
    ghostwu@ghostwu:~/software$ python
    Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> size = 22852956
    >>> print "%s kb" % ( size >> 10 )
    22317 kb
    >>> print "%s MB" % ( size >> 20 )
    21 MB
    >>> 

    6,把列表中的值,连接成字符串

    >>> a = [10, 20, 30, 1, 2, 3]
    >>> s = str( a )
    >>> s
    '[10, 20, 30, 1, 2, 3]'
    >>> type( s )
    <type 'str'>
    >>> s[1:-1]
    '10, 20, 30, 1, 2, 3'
    >>> s.replace( ', ', '', s[1:-1] )
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: an integer is required
    >>> s[1:-1].replace( ', ', '' )
    '102030123'
    >>> 
  • 相关阅读:
    初中几何
    角平分线导致的三角形内外角关系
    解方程
    初中|数学题目整理
    求一次函数解析式
    整式的四则运算
    分式方程
    做辅助线的方法
    线段相等的证明思路
    python2.7安装sqlite3模块
  • 原文地址:https://www.cnblogs.com/ghostwu/p/8684509.html
Copyright © 2020-2023  润新知