码迷,mamicode.com
首页 > 编程语言 > 详细

python基础技巧综合训练题2

时间:2018-03-31 23:16:10      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:def   5.4   window   call   copyright   traceback   运算   install   get   

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( \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  读出命令行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
>>> 

 

python基础技巧综合训练题2

标签:def   5.4   window   call   copyright   traceback   运算   install   get   

原文地址:https://www.cnblogs.com/ghostwu/p/8684509.html

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