python基础技巧综合训练题2

Posted ghostwu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,统计出现次数最多的字符

[email protected]:~/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)]
[email protected]:~/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

[email protected]:~/python/tmp$ 

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

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

[email protected]:~/python/tmp$ !p
python statics.py 
[(be, 3), (is, 10), (than, 8)]
[email protected]:~/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 ] ]
[email protected]:~/python/tmp$ 

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

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

[email protected]:~/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
[email protected]:~/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
[email protected]:~/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的主要内容,如果未能解决你的问题,请参考以下文章

python matplotlib 绘制训练曲线 综合示例——平滑处理图题设置图例设置字体大小线条样式颜色设置

python基础训练题2-元组,字典

1Python进阶强化训练之装饰器使用技巧进阶

冒泡排序这 2 个小技巧,你了解吗?

python matplotlib怎么在一张图上画多条曲线?Python处理多个csv文件生成叠加曲线图——综合示例:平滑处理图注图例图题范围缩放

第2章 Python基础-字符编码&数据类型 综合 练习题