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), ('', 2), ('', 2), ('', 2), ('d', 1), ('l', 1), ('', 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( ', ', '' )
''
>>>
python基础技巧综合训练题2的更多相关文章
- python基础技巧综合训练题1
1,大小写翻转 >>> str='hello,GhostWU' >>> str.swapcase() 'HELLO,gHOSTwu' 2,从一串字符串中,提取纯数字 ...
- python基础之逻辑题(3)
Python基础之逻辑题(3) 1.编写一个函数实现将IP地址转换成一个整数 2.求结果:---lambda 3.求a的结果 4.求下面nums的输出 5.求下面片段的输出 6.写出程序的结果:--- ...
- python基础之逻辑题(2)
python基础之逻辑题(2) 1.若k为整数,下列while循环执行的次数为? 2.请输出正确结果-----numbers? 3.求结果-----math? 4.求结果-----sum? 5.输 ...
- python基础之逻辑题(1)
python基础之逻辑题(1) 1.用一行代码实现数值交换? 2.求结果--fromkeys? 3.1,2,3,4,5能组成多少个互不相同且无重复的三位数? 4.有两个字符串列表a和b,每个字符串是逗 ...
- 面试题之第一部分(Python基础篇) 80题
第一部分(python基础篇)80题 为什么学习Python?==*== # 1. python应用于很多领域,比如后端,前端,爬虫,机器学习(人工智能)等方面,几乎能涵盖各个开发语言的领域,同时它相 ...
- python面试题解析(python基础篇80题)
1. 答:出于编程的喜爱,以及行业本身的前瞻性,创造性,优越性,越是综合的科目越能检验一个人的能力,喜欢这种有挑战的事情. 2. 答:跟随老师学习,以及自己查询资料,结合实战,进行输入输出以及 ...
- python(9)- python基础知识刷题
1. 执行 Python 脚本的两种方式 交互方式:命令行 Windows操作系统下,快捷键cmd,输入“python”启动交互式python解释器. 文件方式:python文件 2. 简述位.字 ...
- python基础面试常见题
1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之一. 2.通过什么途径学习的Python? 通过自学,包括网上查看一些视频,购买一 ...
- python 基础技巧
多个字典合并 >>> d1 = {'name' : 'revotu', 'age' : 99} >>> d2 = {'age' : 24, 'sex' : 'mal ...
随机推荐
- 控制台API函数----HANDLE、SetConsoleCursorPosition、SetConsoleTextAttribute
控制台API函数 调用相关文本界面控制的API函数,这些函数可分为三类. 一.用于控制台窗口控制的函数(包括窗口的缓冲区大小.窗口前景字符和背景颜色.窗口标题.大小和位置等): 二.用于控制台输入输出 ...
- Lua C API 遍历 table
http://timothyqiu.com/archives/lua-note-table-traversal-using-c-api/ C API 遍历 Table lua_getglobal(L, ...
- node - 使用request发送http请求
在nodejs的开发中,有时需要后台去调用其他服务器的接口,这个时候,就需要发送HTTP请求了.有一个简单的工具可以用,Simplified HTTP request client,可以比较方便的模拟 ...
- iOS-贝塞尔连续曲线
一个曲线 UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPath ...
- Twisted 框架 初印象
上面是twisted官网推荐的书籍,从封面可以看到,是一堆大蟒(python)纠缠在一起,这里可以说明twisted是一个基于pyhton语言,支持各种网络协议(包括UDP,TCP,TLS和其他应用层 ...
- python--使用pickle序列化对象
pickle序列化对象 如果希望透明地存储 Python 对象,而不丢失其身份和类型等信息,则需要某种形式的对象序列化:它是一个将任意复杂的对象转成对象的文本或二进制表示的过程. 同样,必须能够将对象 ...
- 10 Tips for Optimizing Your Website’s Speed
转自:http://sixrevisions.com/web-development/site-speed-performance/ Web page speed and performance is ...
- Android 9 新功能 及 API 介绍(提供了实用的模块化的功能支持,包括 人工智能)
Android 9(API 级别 28)为用户和开发者引入了众多新特性和新功能. 本文重点介绍面向开发者的新功能. 要了解新 API,请阅读 API 差异报告或访问 Android API 参考. ...
- vue使用代理实现开发阶段跨域
在config/index.js找到 proxyTable对象,添加键值对即可. "/api":{ target:"http://192.168.1.1", c ...
- Facade外观模式(结构性模式)
1.系统的复杂度 需求:开发一个坦克模拟系统用于模拟坦克车在各种作战环境中的行为,其中坦克系统由引擎.控制器.车轮等各子系统构成.然后由对应的子系统调用. 常规的设计如下: #region 坦克系统组 ...