1. To judge whether each letter in a string is in another string, we can use the characteristics of the set to solve the problem. If the elements of the set exist, it is impossible to add the update to the set again. Then the length of the set is the same as before. If you add it, the length of the set will increase
>>> 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. What if it is more than one character?
#!/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. Count the most frequent characters
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$
Here is a lambda expression. Key specifies which key to sort by. Item is a parameter representing the current tuple. item[1], which is the second item in the tuple. Here is the number of strings. reverse = True, sorting from high to low
4. Count the occurrence times of three words in this module: 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( '\n', '' ) 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 reads the execution result of the python -m this module from the command line into a string
5. Using displacement operator to convert B, KB and MB
ghostwu@ghostwu:~/software$ ls -l sogoupinyin_2.2.0.0102_amd64.deb -rw-rw-r-- 1 ghostwu ghostwu 22852956 2 Month 214: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 Month 214: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. Concatenate the values in the list into strings
>>> 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' >>>